1

I have a class that uses a HttpWebRequest to post some XML and recieve some XML back. It all works great on a windows application but when I use it in an ASP.Net web side I get as WebException "Unable to connect to the remote server". I think that it is something to do with going through my companies Proxy. But am not sure of how to set up the Credentials so that it will work in the web pages. Below is the code that posts the XML (m_Credentials has been set by using "CredentialCache.DefaultCredentials":

    private string PostData(string url, string postData)
    {
      HttpWebRequest request=null;

      Uri uri = new Uri(url);
      request = (HttpWebRequest) WebRequest.Create(uri);
      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";
      request.ContentLength = postData.Length;

      // Tell it to use our credentials else we may not get through 
      if (m_Credentials != null)
      {
        request.Proxy.Credentials = m_Credentials;
      }


      using(Stream writeStream = request.GetRequestStream())
      {
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] bytes = encoding.GetBytes(postData);
        writeStream.Write(bytes, 0, bytes.Length);
      }

      string result=string.Empty;
      using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
      {
        using (Stream responseStream = response.GetResponseStream())
        {
          using (StreamReader readStream = new StreamReader (responseStream, Encoding.UTF8))
          {
            result = readStream.ReadToEnd();
          }
        }
      }
      return result;
    }
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Gav
  • 23
  • 1
  • 4
  • wy are you using `request.Proxy.Credentials` ? you should put credentials not to the proxy .... please explain – Royi Namir Apr 18 '12 at 14:23

3 Answers3

1

Below is the code that posts the XML (m_Credentials has been set by using "CredentialCache.DefaultCredentials"

That might be the issue. On a Windows app, the credentials used to authenticate with the proxy will be the ones of the current logged in user. On the ASP.NET app, the credentials used will be the ones from the local service account running the asp.net process. One option is to set the credentials to an actual valid account on your network.

request.Credentials = new NetworkCredential("username","password","domain");
Icarus
  • 63,293
  • 14
  • 100
  • 115
0

try putting values here :

   request.Credentials = new NetworkCredential(cre.Username, cre.Password, cre.Domain);

you can also use the CredentialCache if you dont want to send each time...)

CredentialCache cache = new CredentialCache();
Uri prefix = new Uri ("http://exchange.somedomain.com");
cache.Add (prefix, "Digest", new NetworkCredential ("joe", "passwd"));
cache.Add (prefix, "Negotiate", new NetworkCredential ("joe", "passwd"));
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0

Problem was traced down to the fact that I needed to set the Proxy for the request

request.Proxy = new WebProxy(m_ProxyAddress);

And the proxy address in IE was to a java scriped that returned the true address to be using

Thanks for the help Gav

Gav
  • 23
  • 1
  • 4