|
-
September 19th, 2011, 06:51 PM
#1
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());
-
September 19th, 2011, 08:08 PM
#2
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.
Always use [code][/code] tags when posting code.
-
September 19th, 2011, 08:12 PM
#3
Re: The underlying connection was closed: An unexpected error occurred on a receive.
Still, once the issue gets fixed. Do you know a solution?
-
September 19th, 2011, 08:24 PM
#4
Re: The underlying connection was closed: An unexpected error occurred on a receive.
 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?
-
September 19th, 2011, 08:31 PM
#5
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.
-
September 19th, 2011, 09:06 PM
#6
Re: The underlying connection was closed: An unexpected error occurred on a receive.
 Originally Posted by kytro360
Im not sure about the Response and Status properties.
...Can you check please?
-
September 19th, 2011, 09:11 PM
#7
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
-
September 20th, 2011, 12:49 PM
#8
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.
-
September 20th, 2011, 02:21 PM
#9
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?
-
September 20th, 2011, 03:43 PM
#10
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.
-
September 20th, 2011, 03:46 PM
#11
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
-
September 20th, 2011, 04:06 PM
#12
Re: The underlying connection was closed: An unexpected error occurred on a receive.
 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.
-
September 20th, 2011, 04:14 PM
#13
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
-
September 20th, 2011, 04:22 PM
#14
Re: The underlying connection was closed: An unexpected error occurred on a receive.
 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.
-
September 20th, 2011, 04:23 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|