我有一個類,它使用HttpWebRequest來發布一些XML並接收一些XML。它在Windows應用程序中都很好用,但當我在ASP.Net Web端使用它時,我得到WebException「無法連接到遠程服務器」。我認爲這與通過我的公司代理服務器有關。但我不確定如何設置證書,以便它可以在網頁中使用。下面是職位XML代碼(m_Credentials已通過使用「CredentialCache.DefaultCredentials」載:在ASP.NET中使用HttpWebRequest
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;
}
返回的真實地址是一個java scriped?你應該把憑據不要代理....請解釋 – 2012-04-18 14:23:51