关闭于HttpWebRequest的POST在生产服务器底层连接 [英] underlying connection Closed on HttpWebRequest POST On production Server

查看:219
本文介绍了关闭于HttpWebRequest的POST在生产服务器底层连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了基础连接已关闭:连接被意外关闭。错误尝试使用在生产服务器上HttpWebRequest类发帖,我开发的机器上正常工作。

I'm getting the "The underlying connection was closed: The connection was closed unexpectedly." error while trying to POST using the HttpWebRequest class on the production server, on my dev machine it works fine.

我最初尝试使用WebClient类,但我切换到HttpWebRequest的尝试一些我发现,而研究的问题(如将keepalive设置为false,preAuthenticate真实的建议
 和ProtocolVersion 1.0)。

I originally tried using the WebClient class but I switched to the HttpWebRequest to try some of the suggestions I found while researching the issue (such as setting KeepAlive to false, PreAuthenticate true and ProtocolVersion to 1.0).

由于它只是在生产服务器上发生的事情,我猜它可能是与IIS。

Since it's only happening on the production server, i'm guessing that it might have something to do with IIS.

下面是我的code

        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();

而code我使用WebClient类原审

And the code I originally tried using the WebClient class

  /*  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); */

谢谢,任何帮助将是AP preciated。

Thanks, Any help would be appreciated.

编辑:

使用菲德勒检查我得到了这个头信息

Using fidler to examine the header I got this info

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中处理安全。这不是一个解决方案,但我希望有人可以使用该信息来帮助我解决这个问题。

If set the Target Framework (I used a new project for testing) to 2.0 (I didn't test every version of the framework) it works. I'm guessing that .net handles the security differently in .net 4.0. This isn't a solution, but I'm hoping somebody could use that info to help me resolve this.

推荐答案

这似乎是一个权限问题。如果我在IIS上更改应用程序池的帐户从ASP.net到NTService它然后正确的职位。我要去尝试,并找出它需要哪些具体的权限,看看我能恩准的asp.net帐户。

It appears to be a permissions issue. If I change the application pool's account on IIS from ASP.net to NTService it then posts correctly. I'm going to try and find out which specific permission it needs and see if I can grant that to the asp.net account.

更新

我不知道究竟是什么做的,但改变了一些用户帐户的写权限的服务器上运行,并重新启动它后,它现在的工作。

I'm not sure what exactly did it, but after changing some of the write permissions for the user account that's running on the server and restarting it, it is now working.

在最后我能够使用Web客户端包装类。

In the end I was able to use the WebClient wrapper class.

    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;

    byte[] resp = client.UploadData(url, "POST", postbytes);

这篇关于关闭于HttpWebRequest的POST在生产服务器底层连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆