用HttpWebRequest POST数据时POST数据被截掉了。使用HTTPLook跟踪出来的HTTP头信息
POST /sp/SPLogin HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 85
Expect: 100-continue
Connection: Keep-Alive
Host: test.com
Cookie: JSESSIONID=Ghdht2vhW2xTPcFRYG1ch1Xt1Kp81rRGvR11nlQNKW6l4MTZ1MHk!-1870340995
正常情况应为:
POST /sp/SPLogin HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322
Accept-Language: zh-cn
UA-CPU: x86
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Content-Length: 85
Connection: Keep-Alive
Host: admin.sh.monternet.com
Cookie: JSESSIONID=Ghdht2vhW2xTPcFRYG1ch1Xt1Kp81rRGvR11nlQNKW6l4MTZ1MHk!-1870340995
selectAccount=SPPREREG&USER=907000&PASSWORD=111111&addCode=9146&Submit22=%B5%C7%C2%BC
以下是程序代码:
public static void GetWebContent(string url,CookieContainer cookies,string filename)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
CookieCollection cookieResponse = new CookieCollection();
CookieContainer cookieRequest = new CookieContainer();
Uri requestUri = new Uri(url);
try
{
Cookie cookie = new Cookie();
cookie.Name = "JSESSIONID";
cookie.Value = "Ghdht2vhW2xTPcFRYG1ch1Xt1Kp81rRGvR11nlQNKW6l4MTZ1MHk!-1870340995";
cookie.Path = "/";
cookie.Domain =requestUri.Host;
cookieRequest.Add(cookie);
// 处理请求
request = (HttpWebRequest) WebRequest.Create(new Uri(url));
request.Accept ="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322";
request.Headers.Add("Accept-Language: zh-cn");
request.Headers.Add("UA-CPU: x86");
request.Headers.Add("Accept-Encoding: gzip, deflate");
request.Timeout = 20000;
request.CookieContainer = cookieRequest;
string data = "selectAccount=SPPREREG&USER=907000&PASSWORD=111111&addCode=9146&Submit22=%B5%C7%C2%BC";
byte[] byteData = Encoding.Default.GetBytes(data);
request.Method ="POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteData.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(byteData,0,byteData.Length);
}
// 处理响应
response = (HttpWebResponse)request.GetResponse();
cookieResponse = response.Cookies ;
byte[] buffer = ResponseAsBytes(response);
response.Close();
FileStream file = new FileStream(filename, FileMode.Create);
file.Write(buffer,0,buffer.Length);
}catch(Exception ex)
{
Log.Error("GetWeb:Url="+url + ex.Message,ex);
}
}
那位知道原因吗??
自己已经搞定,设置一个属性
request.ServicePoint.Expect100Continue = false;
HttpWebRequest POST 数据时请求头多了一行Expect: 100-continue,少了数据行
最新推荐文章于 2024-03-11 15:25:38 发布