-
The underlying connection was closed: An unexpected error occurred on a receive.
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.
This is my httpwebrequest code. I removed the postData and the sites I am trying to request to since I am planning to sell my program and dont want leechers to get a hold of my code.
Code:
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());
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
You got nothing to worry about most leachers would not bother with code that doesn't work.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Still, once the issue gets fixed. Do you know a solution?
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Quote:
Originally Posted by
kytro360
I removed the postData and the sites I am trying to request to since I am planning to sell my program and dont want leechers to get a hold of my code.
Haha, I'm sure you're super-awesome use of .NET library types isn't worth stealing. Yeesh.
What are the values of the "Response" and "Status" properties of your request object? Why are you creating two StreamReaders one after another? Why aren't you closing (Close(), Dispose()) your returned stream?
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Oh wow, the double streamreaders was an error on my part. I removed one. And I closed and disposed my stream. Im not sure about the Response and Status properties.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Quote:
Originally Posted by
kytro360
Im not sure about the Response and Status properties.
...Can you check please?
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Oh by Im not sure I meant I have no clue what you're talking bout when you said Response and Status properties :)
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
The HttpWebRequest type has two properties of interest; Response and Status. Check them after calling GetResponse(). Seriously, the documentation is your friend.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Looked a little bit in Google. Still have no clue what to tell ya bout the Response and Status. Is it necessary to continue?
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
What do you mean you have no clue? Set a breakpoint after calling GetReponse() and check the values of those two properties.
Did you by any chance just copy this code from an example on some site? What is your familiarity with C# and programming in general? I don't mean that to be rude or condescending, I am asking so that I know how to help you better.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
I know the basics of C#. Im not a pro of every aspect of httpwebrequest thats why Im confsued. What do you mean by GetResponse? Also I never understood breakpoints
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Quote:
Originally Posted by
kytro360
Also I never understood breakpoints
Then you don't know the basics. Learn how to use the debugger. As far as what I mean by 'GetResponse()", read your own code:
Code:
response = (HttpWebResponse)request.GetResponse();
Really, I would spend a lot more time learning how to program before attempting a commercial project. This is bound for failure, or even worse; someone buys your application.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
:/ Ill look into a tutorial. I just have never really bothered with debugging code on my own. If I had an issue I would always Google it or ask on a forum.
I knew I had that line of code but I thought you wanted me to add some other code to maybe make a messagebox appear with the properties
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Quote:
Originally Posted by
kytro360
:/ Ill look into a tutorial. I just have never really bothered with debugging code on my own. If I had an issue I would always Google it or ask on a forum.
Which is only a good option if you take the time to understand what other people have handed you. Don't be a Cargo Cult Programmer. There won't always be someone there to hand you an answer when you start tackling problems that are actually difficult.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Im trying to debug it but its not letting me. The error I get is in this line:
Code:
Stream postreqstream2 = request.GetRequestStream();
And I am trying to step over it but its not letting me.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
I don't even believe that you get that far as this line will throw an exception every time because the URI isn't valid.
Code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
You can't pass an empty string into WebRequest.Create(). How do you expect to make a request with an invalid URI? You set a proxy IP, but you never actually tell the request what it is looking for.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
No. In my initial post I said I edited out the details of the request so on my side I can see the site Im connecting too. It was all to keep my code safe and away from leechers.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Oh yes, right... the leechers.
Well, what is the exception that is thrown and what is the InnerException (assuming there is one)?
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
What do you mean by exception? If by exception you mean error then the error I got was:
The underlying connection was closed: An unexpected error occurred on a receive.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
An exception is what is thrown. That is the message from the exception. Per the documentation, and one of these exceptions may be thrown:
Quote:
ProtocolViolationException
The Method property is GET or HEAD.
-or-
KeepAlive is true, AllowWriteStreamBuffering is false, ContentLength is -1, SendChunked is false, and Method is POST or PUT.
InvalidOperationException
The GetRequestStream method is called more than once.
-or-
TransferEncoding is set to a value and SendChunked is false.
NotSupportedException
The request cache validator indicated that the response for this request can be served from the cache; however, requests that write data must not use the cache. This exception can occur if you are using a custom cache validator that is incorrectly implemented.
WebException
Abort was previously called.
-or-
The time-out period for the request expired.
-or-
An error occurred while processing the request.
ObjectDisposedException
In a .NET Compact Framework application, a request stream with zero content length was not obtained and closed correctly. For more information about handling zero content length requests, see Network Programming in the .NET Compact Framework.
I'm guessing it is a WebException, but I don't like guessing. Also, each exception has an InnerException property. Check that and, if it is not null, tell us what type of exception it is and what its message was.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Alright :/ I set up a try catch statement where the catch statement should output the exception in a message. It showed the same error message I said earlier.
Maybe Im trying to get the exception wrong. This is what Im doing:
Code:
try {
///CODE HERE
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
...*sigh*, I'm not interested in the message, I am interested in the exception type. An exception is a type like any other type in your code. It has properties, use them. When the error message pops up click the button at the bottom which says "Exception details". Honestly I really hope you're not intending on selling any software for quite some time.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Exception details? The button popped up showing the error, I looked all over the bottom of the program and the Microsoft C# Visual Studio and did not find an exception details. Are you sure you dont have the professional version or maybe an option that I have turned off?
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Oh now I understand. I removed the try and catch statements and this is what it said. Sorry if Im annoying and slow:
Status: System.Net.WebExceptionStatus.ReceiveFailure
Inner Exception: {"Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."}
Response: null
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
It may be a timeout issue. Try setting the Timout property to a larger value or disabling it completely to see if the call ever returns:
Code:
request.Timeout = -1; // never time out
That said, the default is 100,000 (ms), 100 seconds, so unless the connection is reeeeaaallly slow, that is probably not it.
Unfortunately that error can occur for MANY different reasons. See all the results for this search:
http://www.google.com/search?sourcei...he+remote+host
I wish I had more useful information. Does it work without the proxy? You'll need to start removing variables one by one until it works.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
I tried the timeout code but it still came up with the error. Also, yes the httpwebrequest works without the proxy code.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Ok, so it's something to do with the proxy, at least that's progress. I'm not sure what else to say other than to start tweaking things on the proxy side.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Yeah :/, I looked on the results of the Google search and havent found much good info. Ive also tried several proxies and I put them through a proxy checker to make sure they're alive.
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Okay I think I might have a solution. A while ago I Google my issue and I needed to add this code to my References.cs. Do you know where that file is located at?
Code:
protected override WebRequest GetWebRequest(Uri uri)
{
HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri);
webRequest.KeepAlive = false;
webRequest.ProtocolVersion=HttpVersion.Version10;
return webRequest;
}
Heres a link that talks about it: http://geekswithblogs.net/denis/arch.../16/50365.aspx
-
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Quote:
Originally Posted by
kytro360
Okay I think I might have a solution. A while ago I Google my issue and I needed to add this code to my References.cs. Do you know where that file is located at?
If you don't have a web service proxy, then you won't have a references.cs file.
You don't need to add this method, all you need to do is set up the HttpWebRequest properties the same when you create the object.
Btw, even though you aren't using a web service proxy so this doesn't apply in your case... in general, modifying the auto-generated proxy code is usually a bad idea (because every time you regenerate the code, you'll need to add back in the changes).