I am trying to do a httpwebrequest with a proxy and everytime I try to do it I get this error:

The underlying connection was closed: An unexpected error occurred on a receive.

Dani AI

Generated

A few practical notes based on the snippet you posted and the follow-ups from and .

The exception is a generic socket/transport failure; three things are most commonly responsible here: a bad proxy configuration (wrong host/port or missing credentials), incorrect handling of the response stream (opening/reading it more than once or not disposing it), and TLS/protocol mismatches between client and server. The problem you show (two StreamReaders created from the same response stream and no using/dispose around the response) is a classic cause — unread/undrained response data or double-reading can lead the underlying socket to be closed. (stackoverflow.com)

Quick checklist (try them in order):

  • Confirm the proxy is actually correct: test the same request with no proxy (or use the system/default proxy) to see if the error goes away; a bad proxy variable is a frequent root cause (see ). (learn.microsoft.com)
  • Read the response stream exactly once and dispose everything with using. If you need the body more than once, copy it into a MemoryStream first. Example pattern:
using (var resp = (HttpWebResponse)request.GetResponse())
using (var rs = resp.GetResponseStream())
using (var sr = new StreamReader(rs))
{
  var body = sr.ReadToEnd();
}

This ensures sockets are returned/released promptly. (stackoverflow.com)

  • If the target is HTTPS, ensure the client and server can agree on TLS versions (set or allow TLS1.2+ as appropriate and do it before creating the request, or prefer HttpClient with a configured handler). On modern servers this is a very common cause. (learn.microsoft.com)

If those do not show the culprit, enable a network trace (Fiddler/Wireshark) and check server logs to see which side is closing the TCP connection; that will point you to proxy/auth, TLS handshake, or server-side timeouts. (stackoverflow.com)

If a sanitized minimal repro can be posted (no secret URLs or data), it will make diagnosis faster — but the above fixes resolve most cases.

Recommended Answers

All 7 Replies

Would it be possible for you to show the code you are using? It's hard to debug code without actually seeing it.

I could show the code but Im removing the postdata of the request and the site that I am httpwebrequesting to since I am planning to sell the software and dont want leechers to get their hands on it. Hope you understand.

string MyProxyHostString = txtBoxIP.Text;
int MyProxyPort = System.Convert.ToInt32(txtBoxPort.Text);

System.Net.ServicePointManager.Expect100Continue = false;

string postdata = "";


 UTF8Encoding encoding1 = new UTF8Encoding();
byte[] bytedata = encoding1.GetBytes(postdata);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
 request.Proxy = new WebProxy(MyProxyHostString, MyProxyPort);
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.Referer = "";
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
request.ContentLength = bytedata.Length;



Stream postreqstream2 = request.GetRequestStream();
postreqstream2.Write(bytedata, 0, bytedata.Length);
postreqstream2.Close();
HttpWebResponse response = default(HttpWebResponse);

response = (HttpWebResponse)request.GetResponse();
StreamReader stream2 = new StreamReader(response.GetResponseStream());
StreamReader reader2 = new StreamReader(response.GetResponseStream());

Bump....anyone?

Bump, I need help still :/

Bumnpppppppppppppp

You ever get the solution to this? I'm having the same problem when i use a proxy..

Never mind, figured out my problem.. Was passing a bad variable to httpwerequest proxy value..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.