请高手支招啊。
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 方法访问。