HttpWebRequest发送数据 post
2012-11-28 15:44
519 查看
1、创建HttpWebRequest对象
HttpWebRequest对象用WebRequest.Create(url)方法获得。
2、初始化HttpWebRequest对象(设置http请求常用属性)
3、附加Post给服务器的数据到HttpWebRequest对象(写入HttpWebRequest对象提供的一个stream里面)
4、读取服务器的返回信息(注意返回数据的Encoding)
string url = "";
Encoding encoding = Encoding.GetEncoding("utf-8");
byte[] bytesToPost = encoding.GetBytes("我学习WebRequest");
string cookieheader = string.Empty;
CookieContainer cookieCon = new CookieContainer();
#region 创建HttpWebRequest对象
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
#endregion
#region 初始化HtppWebRequest对象
httpRequest.CookieContainer = cookieCon;
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.Method = "POST";
if (cookieheader.Equals(string.Empty))
{
cookieheader = httpRequest.CookieContainer.GetCookieHeader(new Uri(Url));
}
else
{
httpRequest.CookieContainer.SetCookies(new Uri(Url), cookieheader);
}
#endregion
#region 附加Post给服务器的数据到HttpWebRequest对象
httpRequest.ContentLength = bytesToPost.Length;
System.IO.Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytesToPost, 0, bytesToPost.Length);
requestStream.Close();
#endregion
#region 读取服务器返回信息
System.IO.Stream responseStream = httpRequest.GetResponse().GetResponseStream();
string stringResponse = string.Empty;
using (System.IO.StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
{
stringResponse = responseReader.ReadToEnd();
}
responseStream.Close();
#endregion
来源于:www.hackbadboy.com BadBoy网络安全小组
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接:
http://blog.csdn.net/mypc2010/article/details/7070470
HttpWebRequest对象用WebRequest.Create(url)方法获得。
2、初始化HttpWebRequest对象(设置http请求常用属性)
3、附加Post给服务器的数据到HttpWebRequest对象(写入HttpWebRequest对象提供的一个stream里面)
4、读取服务器的返回信息(注意返回数据的Encoding)
string url = "";
Encoding encoding = Encoding.GetEncoding("utf-8");
byte[] bytesToPost = encoding.GetBytes("我学习WebRequest");
string cookieheader = string.Empty;
CookieContainer cookieCon = new CookieContainer();
#region 创建HttpWebRequest对象
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
#endregion
#region 初始化HtppWebRequest对象
httpRequest.CookieContainer = cookieCon;
httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.Method = "POST";
if (cookieheader.Equals(string.Empty))
{
cookieheader = httpRequest.CookieContainer.GetCookieHeader(new Uri(Url));
}
else
{
httpRequest.CookieContainer.SetCookies(new Uri(Url), cookieheader);
}
#endregion
#region 附加Post给服务器的数据到HttpWebRequest对象
httpRequest.ContentLength = bytesToPost.Length;
System.IO.Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytesToPost, 0, bytesToPost.Length);
requestStream.Close();
#endregion
#region 读取服务器返回信息
System.IO.Stream responseStream = httpRequest.GetResponse().GetResponseStream();
string stringResponse = string.Empty;
using (System.IO.StreamReader responseReader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
{
stringResponse = responseReader.ReadToEnd();
}
responseStream.Close();
#endregion
来源于:www.hackbadboy.com BadBoy网络安全小组
$(document).ready(function(){dp.SyntaxHighlighter.HighlightAll('code');});
原文链接:
http://blog.csdn.net/mypc2010/article/details/7070470
相关文章推荐
- 用httpwebrequest/httpwebresponse向服务器POST发送及接收数据
- HttpWebRequest发送Post数据
- HttpWebRequest发送数据 post
- HttpWebRequest Post 方式发送 XML数据
- C# httpwebrequest post 发送的数据 urlencode 问题
- HttpWebRequest post "multipart/form-data"类型的web数据
- C#POST数据,HttpWebRequest请求页面,HttpWebResponse返回数据
- HttpWebRequest POST 数据时请求头多了一行Expect: 100-continue,少了数据行
- HttpWebRequest 向网站提交数据 (post与get方式)
- HttpWebRequest GET 方式发送数据
- HttpWebRequest POST 数据时请求头多了一行Expect: 100-continue,少了数据行
- 使用HttpWebRequest POST数据到服务器上
- 黄聪:C#模拟网站页面POST数据提交表单(二)--HttpWebRequest以及HttpWebResponse (转)
- (转载)利用HttpWebRequest通过POST Json数据在.net后台实现不同平台间的数据传输
- HttpWebRequest post数据[xgluxv]
- C#利用HttpWebRequest,Stream,HttpWebResponse,StreamReader获取post返回的数据
- webrequest post 数据 编码 HttpWebRequest get
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- HttpWebRequest 发送POST请求的分析
- 使用HttpWebRequest post数据时要注意UrlEncode