Click here to Skip to main content
15,176,948 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello everyone

I am posting the same question again. Because i did not get any reply from my previous question.
I am working on an application. Its a window application which developed in .net 2.0

Now the problem is this that when ever i run my application and i send a request to another application its send successfully but i get the problem when i get the response its show me the error and the error is

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

here is my code

C#
public string tm4b(string sURL, bool bEscapeURL, string sPostData)
{
sURL = sURL.Trim();
while (sURL.IndexOf(" ") > 0)
{
sURL = sURL.Replace(" ", " ");
}
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string stmp = string.Empty;
Uri httpUri = new Uri(sURL, bEscapeURL);
try
{
 
System.Text.ASCIIEncoding byteConverter = new System.Text.ASCIIEncoding();
byte[] byte1 = byteConverter.GetBytes(sPostData);
System.Net.HttpWebRequest hwRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(httpUri);
 
hwRequest.ContentType = "application/x-www-form-urlencoded";
hwRequest.Method = "POST";
 
hwRequest.ContentLength = sPostData.Length; ;
System.IO.Stream PostStream = hwRequest.GetRequestStream();
PostStream.Write(byte1, 0, byte1.Length);
System.Net.HttpWebResponse hwResponse = (System.Net.HttpWebResponse)hwRequest.GetResponse();
System.IO.StreamReader sRead = new System.IO.StreamReader(hwResponse.GetResponseStream(),
System.Text.Encoding.ASCII);
 
if ((stmp = sRead.ReadLine()) != null)
{
 
sb.Append(stmp + "");
 
}
 
PostStream.Close();
sRead.Close();
 
return sb.ToString();
 
}
catch (Exception ex)
{
 
return "error";
}
return "Request Error";
 
}



But its give me this error for only one url and for anothers url its working well

Please check the code and error and please tell me what i have to do
i tried all the logics that i can use but did not get any good results

So experts please help me
Posted
Updated 15-Jun-21 21:14pm
v2

If your service is https then add this line before webclient call

ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
   
Comments
Member 13809163 2-Feb-20 13:20pm
   
Thanks alot.

This works--
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
Member 14842549 24-May-20 22:30pm
   
this also works for me.
Drew Thomas 11-Jun-20 13:02pm
   
Worked for me as well. Thanks so much
gggustafson 27-Feb-21 14:59pm
   
Worked for me. My placement was immediately after static void Main ( ) in a WinForm project. Note too to include using System.Net
Kanakaraju Naidu 2-Jun-21 1:36am
   
this works for my console application
ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
Thank you...
"But its give me this error for only one url and for anothers url its working well" - that's the important point. Which URL is it? Does the URL work when you use it from a common web browser (e.g. Internet Explorer)? When the server decides to close the connection, you'll get an error in your client code, and that is actually correct behavior of your client. If the server is also yours, check what the server is doing with that URL.
   
Comments
Azad R Chouhan 25-Apr-13 7:59am
   
I use fidler on machine in which the application is running and add this code in app config
<system.net>
<settings>
<httpwebrequest useunsafeheaderparsing="true">



Its working then but when i exit the fidler then its give me error what is the reason please tell me
This is related to TLS, Just upgrade the .net to the latest version.

This happens when the server denies tlsv1.0 or tlsv1.0 is disabled from the server side.
To resolve this from the client side just upgrade to the latest version and see.
   
I use fidler on machine in which the application is running and add this code in app config



HTML
<system.net>
<settings>
<httpwebrequest useunsafeheaderparsing="true" />
</settings>
</system.net>



Its working then but when i exit the fidler then its give me error what is the reason please tell me
   
Comments
saurabhbhatt87 31-Dec-16 3:20am
   
i never found satisfactory answer from code.
Member 1866318 12-Oct-18 1:43am
   
Not Working on Window application

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900