Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3457630
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:53:01+00:00 2026-05-18T09:53:01+00:00

I’m getting the The underlying connection was closed: The connection was closed unexpectedly. error

  • 0

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.

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).

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

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

Thanks, Any help would be appreciated.

EDIT:

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T09:53:02+00:00Added an answer on May 18, 2026 at 9:53 am

    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.

    UPDATE

    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.

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.