C# 通过HttpWebRequest post 方法获取cookie
public string SendDataByPost(string Url, string postDataStr, ref CookieContainer cookie)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
if (cookie.Count == 0)
{
request.CookieContainer = new CookieContainer();
cookie = request.CookieContainer;
}
else
{
request.CookieContainer = cookie;
}
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataStr.Length;
Stream myRequestStream = request.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(postDataStr);
myStreamWriter.Close();
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;
} 展开
给你一个新HttpRequest类,我以前用来做社区游戏辅助用的。
namespace RainyManor.Common
{
public class myHttpRequest
{
private CookieContainer _cc = new CookieContainer();
public myHttpRequest(CookieContainer cc)
{
if (cc != null)
_cc = cc;
}
public CookieContainer cookies
{
get { return _cc; }
}
private HttpWebRequest GetRequest(string uri, string method)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = method;
request.Accept = Resources.Accept;
request.UserAgent = Resources.UserAgent;
request.ContentType = Resources.ContentType;
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Timeout = 20000;
return request;
}
public string GetPageSource(string url)
{
HttpWebRequest request = GetRequest(url, "get");
request.CookieContainer = _cc;
return GetStringFromRequest(request, myEncoding.GB2312);
}
private string GetStringFromRequest(HttpWebRequest request, Encoding encode)
{
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(stream, encode))
{
return reader.ReadToEnd();
}
}
}
}
catch
{
return string.Empty;
}
}
public string GetSourceByPost(string url, string postData)
{
try
{
byte[] bytes = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = GetRequest(url, "post");
request.CookieContainer = _cc;
Stream sendStream = request.GetRequestStream();
sendStream.Write(bytes, 0, bytes.Length);
sendStream.Close();
return GetStringFromRequest(request, myEncoding.GB2312);
}
catch
{
return "";
}
}
public string GetSourceByPostAction(string url, string postData)
{
try
{
byte[] bytes = Encoding.ASCII.GetBytes(postData);
HttpWebRequest request = GetRequest(url, "post");
request.ContentType = "text/xml";
request.CookieContainer = _cc;
Stream sendStream = request.GetRequestStream();
sendStream.Write(bytes, 0, bytes.Length);
sendStream.Close();
return GetStringFromRequest(request, myEncoding.UTF8);
}
catch
{
return "";
}
}
}
}
1. Resources.Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
2. Resources.UserAgent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; MS-RTC LM 8)
3. Resources.ContentType:application/x-www-form-urlencoded