请问这里的response.Cookies和cookie.GetCookieHeader(new Uri(Url));有什么不同吗?
response.Cookies应该是提交数据后 网站返回给我们的cookie吧,那cookie.GetCookieHeader(new Uri(Url));不也是网站返回来的cookie吗?
[C#] 纯文本查看 复制代码 public static string HttpPOST(string Url, string proxystr, string postDataStr, string refrere, bool isutf8, bool sendmode, ref CookieContainer cookie, int timeout, string useragent, bool isPostOnGzip)
{
string cstr = "提交前Cookie:" + cookie.GetCookieHeader(new Uri(Url)); //定义字符串保存提交前后的cookie数据,用于分析
Stream myResponseStream = null; //定义服务器返回流
HttpWebResponse response = null; //定义服务器返回的请求
HttpWebRequest request = null;//定义提交请求
try
{
WebProxy proxy = new WebProxy();
System.Net.ServicePointManager.Expect100Continue = false;//当要POST的数据大于1024字节的时候,是否分成两步
System.Net.ServicePointManager.DefaultConnectionLimit = 1000;//设置最大并发连接,避免同时多个提交时出现失败
request = (HttpWebRequest)WebRequest.CreateDefault(new Uri(Url));//根据当前url初始化请求
request.ServicePoint.Expect100Continue = false;////当要POST的数据大于1024字节的时候,是否分成两步
if (proxystr.Length > 0)
{
proxy = new WebProxy(proxystr);
request.Proxy = proxy;
}
request.CookieContainer = cookie;//设置cookie容器
request.Method = "POST";//设置提交方式
if (isPostOnGzip)
request.AutomaticDecompression = DecompressionMethods.GZip;
if (sendmode)//如果是内容提交,则加上此格式,这个是必须的,否则类似文章发布这样的提交无法被服务器识别
{
string split = postDataStr.Substring(postDataStr.IndexOf("-----------------------------") + 29);
split = split.Remove(split.IndexOf("\n"));
request.ContentType = "multipart/form-data; boundary=---------------------------" + split;
}
else
{
request.ContentType = "application/x-www-form-urlencoded";//普通提交例如登录这样的,加上此格式
}
request.Referer = refrere;//设置来路
request.Timeout = timeout * 1000;//设置超时时间,毫秒
request.AllowAutoRedirect = false;//是否跟随重定向
request.Accept = "*/*";//设置标头
request.Headers.Add("Accept-Language", "zh-CN");//设置标头
request.Headers.Add("Cache-Control", "no-cache");//设置标头
request.UserAgent = useragent;//设置标头,客户端信息
byte[] requestBytes = isutf8 ? System.Text.Encoding.UTF8.GetBytes(postDataStr) : System.Text.Encoding.Default.GetBytes(postDataStr);//根据指定的编码将要提交的数据变成字节数组
request.ContentLength = requestBytes.Length;//设置请求的长度
string header = request.Headers.ToString();//设置请求的头
Stream myRequestStream = request.GetRequestStream();//获取提交流
using (Stream writer = myRequestStream)
{
writer.Write(requestBytes, 0, requestBytes.Length);//提交数据到服务器
}
response = (HttpWebResponse)request.GetResponse();//获取服务器返回
myResponseStream = response.GetResponseStream();//获取服务器返回流
if (response.ContentEncoding.ToLower().Contains("gzip"))//如果是gzip,则用gzip的方式解压流
{
myResponseStream = new GZipStream(myResponseStream, CompressionMode.Decompress);
}
else if (response.ContentEncoding.ToLower().Contains("deflate"))
{
myResponseStream = new DeflateStream(myResponseStream, CompressionMode.Decompress);
}
string retString = isutf8 ? (new StreamReader(myResponseStream, Encoding.UTF8)).ReadToEnd() : (new StreamReader(myResponseStream, Encoding.Default)).ReadToEnd();//根据指定的编码将服务器返回流转换成字符串
string session = "";
foreach (Cookie ck in response.Cookies)
{
session += ck.Name + "=" + ck.Value + "; ";
}
string abc = session;
cstr += "\r\n提交后Cookie:" + cookie.GetCookieHeader(new Uri(Url));//拼接提交后cookie,用于分析
retString = retString + "\r\n\r\n=================================================\r\n\r\n本次请求:" + Url + " 响应结果:" + response.StatusCode + "\r\n\r\n" + cstr + "\r\n" + request.CookieContainer.GetCookieHeader(new Uri(Url)) + "\r\nrequest:\r\n" + header + "\r\nresponse:\r\n" + response.Headers.ToString() + "\r\n\r\n=================================================\r\n\r\n";
UpdateCookie(ref cookie, response.Cookies);//将post成功以后得到的新cookie放入cookie容器里面
return retString;
}
catch (Exception ex)
{
return ex.Message;//如果报错,返回错误信息
}
finally
{//最后关闭各种流,各种请求,这里不管报不报错都会最终执行
if (myResponseStream != null)
myResponseStream.Close();
if (response != null)
response.Close();
if (request != null)
request.Abort();
}
}
|