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;
}