HttpWebRequest POST 数据时请求头多了一行Expect: 100-continue,少了数据行

dgvictor 2006-03-18 11:26:50
用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);
}
}
那位知道原因吗??
...全文
2006 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgvictor 2006-03-20
  • 打赏
  • 举报
回复
自己已经搞定,设置一个属性
request.ServicePoint.Expect100Continue = false;

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧