Http Post Error: A connection attempt failed because the connectedparty did not properly respond aft

  • Thread starter Thread starter Deepu
  • Start date Start date
D

Deepu

Hi all,

I am facing HTTP POST problem:

The following piece of code for Http Post works fine if i execute it
in Visual Studio 8.0 in an Console Application.
Whereas if i execute the same code in a window service it gave me
following error:

System.Net.WebException:Unable to connect to remote server.
System.Net .Sockets.SocketException: A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond.

The code is as follows:

string url = "some url";
Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create
(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Proxy = WebRequest.DefaultWebProxy;
request.Proxy.Credentials =
CredentialCache.DefaultCredentials;
try
{
using (Stream writeStream = request.GetRequestStream
())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes("CA761232");
writeStream.Write(bytes, 0, bytes.Length);
}
}
catch (Exception ex)
{
Logger.Current.Error(ex.ToString());
}

string result = string.Empty;
try
{
using (HttpWebResponse response = (HttpWebResponse)
request.GetResponse())
{
using (Stream responseStream =
response.GetResponseStream())
{
using (StreamReader readStream = new
StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
Logger.Current.Info("Output recieved from service as:
" + result);
}
catch (Exception ex)
{
Logger.Current.Error("Error while receiving: " +
ex.Message);
Logger.Current.Error("Error: " + ex.StackTrace);
}

Please reply me as earliest as i had put a lot of head already in
that.....

Thanks in advance
Deepu
 
Deepu said:
Hi all,

I am facing HTTP POST problem:

The following piece of code for Http Post works fine if i execute it
in Visual Studio 8.0 in an Console Application.
Whereas if i execute the same code in a window service it gave me
following error:

System.Net.WebException:Unable to connect to remote server.
System.Net .Sockets.SocketException: A connection attempt failed
because the connected party did not properly respond after a period of
time, or established connection failed because connected host has
failed to respond.

The code is as follows:

string url = "some url";
Uri uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create
(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.Proxy = WebRequest.DefaultWebProxy;
request.Proxy.Credentials =
CredentialCache.DefaultCredentials;
try
{
using (Stream writeStream = request.GetRequestStream
())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes("CA761232");
writeStream.Write(bytes, 0, bytes.Length);
}
}
catch (Exception ex)
{
Logger.Current.Error(ex.ToString());
}

string result = string.Empty;
try
{
using (HttpWebResponse response = (HttpWebResponse)
request.GetResponse())
{
using (Stream responseStream =
response.GetResponseStream())
{
using (StreamReader readStream = new
StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
Logger.Current.Info("Output recieved from service as:
" + result);
}
catch (Exception ex)
{
Logger.Current.Error("Error while receiving: " +
ex.Message);
Logger.Current.Error("Error: " + ex.StackTrace);
}

Please reply me as earliest as i had put a lot of head already in
that.....

Thanks in advance
Deepu

Does the service run under the same account as the console application? In
all likelyhood it doesn't, so that would be where to look first.

Mike
 
Back
Top