我收到“底层连接已关闭:连接意外关闭”。尝试使用生产服务器上的HttpWebRequest类POST时发生错误,在我的开发计算机上工作正常。底层连接在HttpWebRequest POST上关闭生产服务器
我最初尝试使用WebClient类,但我切换到HttpWebRequest尝试在研究问题时发现的一些建议(例如将KeepAlive设置为false,PreAuthenticate true 和ProtocolVersion为1.0)。
由于它只发生在生产服务器上,我猜测它可能与IIS有关。
这里是我的代码
HttpWebRequest HttpWReq =
(HttpWebRequest)WebRequest.Create(webURL);
ASCIIEncoding encoding=new ASCIIEncoding();
Byte[] postbytes = Encoding.ASCII.GetBytes(data);
HttpWReq.Headers.Add("Authorization",
String.Format("Basic {0}", authstring));
HttpWReq.KeepAlive = false;
HttpWReq.PreAuthenticate = true;
HttpWReq.Credentials = CredentialCache.DefaultCredentials;
HttpWReq.UseDefaultCredentials = true;
HttpWReq.ProtocolVersion = HttpVersion.Version10;
HttpWReq.Method = "POST";
HttpWReq.ContentType = "application/x-www-form-urlencoded";
HttpWReq.ContentLength = postbytes.Length;
Stream newStream = HttpWReq.GetRequestStream();
newStream.Write(postbytes, 0, postbytes.Length);
newStream.Close();
我使用WebClient类
/* WebClient client = new WebClient();
client.Headers.Add("Authorization",
String.Format("Basic {0}", authstring));
client.Headers.Add("Content-Type",
"application/x-www-form-urlencoded");
client.UseDefaultCredentials = true;
System.Net.ServicePointManager.Expect100Continue = false;
Byte[] postbytes = Encoding.ASCII.GetBytes(data);
byte[] resp = client.UploadData(webURL, "POST", postbytes); */
由于最初试图代码,任何帮助,将不胜感激。
编辑:
使用菲德勒检查我得到这个信息
POST [MyWebSite] HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave- flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
Referer: [MyWebSite]
Accept-Language: en-us
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB6.5; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322)
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: [MyHost]
Content-Length: 188
Connection: Keep-Alive
Pragma: no-cache
Cookie: __utma=62854692.1254171006.1276438746.1289273298.1289317993.21; __utmz=62854692.1277743505.3.3.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=yeled; ASPSESSIONIDQQQRCRAT=ANNHGGNBNOFNFCLHPBEJIMLC
__VIEWSTATE=%2FwEPDwUKMjA0OTM4MTAwNGRktZi0IsIUo6MOCYTxun8p8Po4AWeTtipGZ4L9%2FkY3KZU%3D&__EVENTVALIDATION=%2FwEWAgLHhsXtBwK14deQBbiFCpWBnsp%2BicqBy%2FNXAkhuVDX9WF1jZayRuTgPc3Ov&btnTest=Test
EDIT2
如果设定的目标框架头(我用于测试一个新的项目)到2.0(我没有测试每个版本的框架)它的工作原理。我猜测.net在.net 4.0中处理安全性不同。这不是一个解决方案,但我希望有人可以使用该信息来帮助我解决这个问题。
我试着设置HttpWReq.Expect =“”它似乎没有作出差别。我不确定如何寻找与提琴手,但我更新我的信息与该信息。 – Jack 2010-11-23 20:11:07