请高手支招啊。

qugui 2007-03-27 10:13:11
还是HttpWebRequest.Expectd在post的时候出现的问题。


我在网上看到有人说这个一个BUG,地址http://www.dotnet247.com/247reference/msgs/31/158004.aspx
但是我用2005出来的程序一样在post的时候会出现Expect: 100-continue。
这样会比IE post数据的时候多一个服务器响应。

我的代码改成如下的时候:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.PreAuthenticate = true;
req.KeepAlive = true;
req.ProtocolVersion = HttpVersion.Version11;


req.Method = "POST";
//req.Timeout = 60000;
req.Accept="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
req.Referer = referer;
req.Headers.Add("Accept-Language: zh-cn");
req.ContentType = "application/x-www-form-urlencoded";
req.Headers.Add("UA-CPU", "x86");
req.AllowAutoRedirect = true;
req.Headers.Add("Accept-Encoding: gzip, deflate");
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
req.Headers.Add("Cache-Control: no-cache");

StringBuilder UrlEncoded = new StringBuilder();
Char[] reserved = { '?', '=', '&' };
byte[] SomeBytes = null;

if (paramList != null)
{
int i = 0, j;
while (i < paramList.Length)
{
j = paramList.IndexOfAny(reserved, i);
if (j == -1)
{
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length - i)));
break;
}
UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j - i)));
UrlEncoded.Append(paramList.Substring(j, 1));
i = j + 1;
}
SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
req.ContentLength = SomeBytes.Length;

Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
req.KeepAlive = true;
req.Expect = null;
req.KeepAlive = true;

string[] key1 = req.Headers.AllKeys;
foreach (string l in key1)
{
strResult += l + ":" + req.Headers[l] + "\r\n";
}
返回的header中并没有了Expect,但是服务器的还在可以从当前请求中读出Expect: 100-continue.
请高人指教啊。

我的测试网址为:http://quack.oicp.net/header.aspx,请用post 方法访问。
...全文
272 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qugui 2007-03-27
  • 打赏
  • 举报
回复
十分感谢!!!
Red_angelX 2007-03-27
  • 打赏
  • 举报
回复
加上这句就没有这个Expect了 代码可以改回原来的了
也不能算是bug 反正都要加这句
Red_angelX 2007-03-27
  • 打赏
  • 举报
回复
加上
ServicePointManager.Expect100Continue = false;
Expect: 100-continue。就没了
Red_angelX 2007-03-27
  • 打赏
  • 举报
回复
我翻下代码
wshuangminlg 2007-03-27
  • 打赏
  • 举报
回复
帮LZ顶

111,094

社区成员

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

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

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