C#模拟http 发送post或get请求
private string HttpPost( string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.Method = "POST" ;
request.ContentType = "application/x-www-form-urlencoded" ;
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
request.CookieContainer = cookie;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding( "gb2312" ));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Cookies = cookie.GetCookies(response.ResponseUri);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding( "utf-8" ));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
public string HttpGet( string Url, string postDataStr)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?" ) + postDataStr);
request.Method = "GET" ;
request.ContentType = "text/html;charset=UTF-8" ;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding( "utf-8" ));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
在post的时候有时也用的到cookie,像登录163发邮件时候就需要发送cookie,所以在外部一个cookie属性随时保存 CookieContainer cookie = new CookieContainer();
!注意:有时候请求会重定向,但我们就需要从重定向url获取东西,像QQ登录成功后获取sid,但上面的会自动根据重定向地址跳转。我们可以用:
request.AllowAutoRedirect = false;设置重定向禁用,你就可以从headers的Location属性中获取重定向地址
C#模拟POST登录cnblogs并发布文章
用到的工具FireFox的Firebugs插件 打开网络功能进行抓包 数据如下
可以得知POST的数据为:
|
在VS里画登陆图
查看别人的代码 登录代码:
s
tring postData =string.Format( "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=/wEPDwULLTE1MzYzODg2NzZkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYBBQtjaGtSZW1lbWJlcm1QYDyKKI9af4b67Mzq2xFaL9Bt&__EVENTVALIDATION=/wEWBQLWwpqPDQLyj/OQAgK3jsrkBALR55GJDgKC3IeGDE1m7t2mGlasoP1Hd9hLaFoI2G05&tbUserName={0}&tbPassword={1}&btnLogin=登 录&txtReturnUrl=http://home.cnblogs.com/",txtName.Text.Trim(),txtPwd.Text.Trim());
//这是cnblogs提交时的一些参数
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://passport.cnblogs.com/login.aspx");
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.KeepAlive = true;
request.CookieContainer = container; //返回的cookie会附加在这个容器里面
//发送数据
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
newStream = response.GetResponseStream();
StreamReader reader = new StreamReader(newStream, Encoding.UTF8);
string text = reader.ReadToEnd();
reader.Close();
newStream.Close();