Httpwebrequest代理身份验证与.net框架3.5返回407错误
本文关键字:返回 错误 框架 代理 身份验证 net Httpwebrequest | 更新日期: 2023-09-27 18:18:25
我在。net framework 3.5项目中使用这段代码:
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(ServiceUri);
WebProxy proxy = new WebProxy();
proxy.Address = new Uri(ProxyUri);
proxy.Credentials = new NetworkCredential(userName, pass, domain);
request.Proxy = proxy;
request.Method = "POST";
request.ContentType = "application/xml";
request.Accept = "application/xml";
byte[] content = Encoding.UTF8.GetBytes(xmlRequest.ToString());
request.ContentLength = content.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(content, 0, content.Length);
}
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
最后一行给出了"需要407代理身份验证"。但如果我添加到config:
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
不使用clr 4.0如何使其工作?
Sun,
我解决这个问题的方法:
proxy.UseDefaultCredentials = true;