Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webrequest not working in a Windows Service

Tags:

c#

I am not able to use Webrequest in a windows service. It fails with error ""Unable to connect to the remote server".

  WebRequest request = WebRequest.Create(url);
  NetworkCredential nc = new NetworkCredential("myuname","mypassword","mydomain");
  request.Proxy.Credentials = nc;

  try
  {

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();
  }
  catch (WebException ex)
  {
    //ex.Message is "Unable to connect to the remote server"
  }

The code works perfectly fine if it is a console application.

Could someone please tell if there is a fix?

like image 579
paseena Avatar asked Dec 18 '25 14:12

paseena


1 Answers

You might want to try specifying the proxy server on the request object you've created. When you run the console application I believe that the system looks up your IE configuration for you and the proxy may be set. If the service is running under an account other than yours it might not have the proxy set.

like image 173
Brian Lyttle Avatar answered Dec 21 '25 02:12

Brian Lyttle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!