Basic connection Closed by HttpWebRequest POST On the production server

I get the message "The main connection was closed: the connection was unexpectedly closed." an error occurred while trying to POST using the HttpWebRequest class on a production server, it works fine on my dev machine.

At first I tried to use the WebClient class, but I switched to HttpWebRequest to try some of the suggestions I found while investigating the problem (for example, setting KeepAlive to false, PreAuthenticate true and ProtocolVersion to 1.0).

Since this only happens on the production server, I assume it might have something to do with IIS.

Here is my 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();

And the code that I originally tried to use 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); */

Thanks, any help would be appreciated.

EDIT:

fidler ,

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

Target Framework ( ) 2.0 ( ), . , .net - .net 4.0. , , - , .

+3
2

. IIS ASP.net NTService, . , , , asp.net.

UPDATE

, , , , , .

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;

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

100-. , , , , .

, Fiddler, , , . , Expect:100-continue. HttpWReq.Expect="";

, Windows, , , - , , , .

UPDATE

, , , . , , HttpWebRequest:

Mozilla/4.0 (; MSIE 7.0; Windows NT 5.1; Trident/4.0; GTB6.5; InfoPath.2;.NET CLR 2.0.50727;. CLR 3.0.4506.2152;.NET CLR 3.5.30729;.NET CLR 1.1.4322)

, , . , 1.1.

, , . request.GetResponseStream .

+2

Source: https://habr.com/ru/post/1776529/


All Articles