HttpWebResponse 远程服务器返回错误: (407) 需要代理身份验证。

本文提供了一种解决本地IIS配置导致的407错误的方法,通过使用代理身份验证来解决远程服务器认证问题。详细介绍了如何创建WebProxy对象,并设置相应的网络凭证,最终确保HTTP请求成功完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

天在公司 爬自己本地iis是出现一个 错误提示“ 远程服务器返回错误: (407) 需要代理身份验证” ,现在把解决方案提出来         static bool GetRequest(string url)
        {
            WebProxy proxy = new WebProxy("10.16.8.52:80", false);
            proxy.Credentials = new NetworkCredential("majiang", "****", "Vancl");

            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
            request.AllowAutoRedirect = false;
            request.Proxy = proxy;
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            response.Close();
            return response.StatusCode == HttpStatusCode.OK;
        }

转载于:https://www.cnblogs.com/majiang/archive/2012/10/09/2716567.html

private void btnModify_Click(object sender, EventArgs e) { string url = "http://192.168.1.104/Movies/Edit/5"; HttpWebRequest request = null; request = WebRequest.Create(url) as HttpWebRequest; request.Accept = "text/html, application/xhtml+xml, image/jxr, *"; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586"; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.Host = "192.168.1.104"; request.KeepAlive = true; request.Headers.Add("Pragma", "no-cache"); request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.Headers.Add("Accept-Language", "zh-CN"); //没有下面两行代码 请求得不到相应 会出现错误提示:远程服务器返回错误: (500) 内部服务器错误。 request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(GV_CookieCollection); StringBuilder sb = new StringBuilder(); //__RequestVerificationToken 参数中没有它也是不行的,你可以试试 sb.Append("__RequestVerificationToken="); sb.Append(hidRequestVerificationToken); sb.Append("&ID=5"); sb.Append("&Title=速度与激情4"); sb.Append("&ReleaseDate=2009-04-03"); sb.Append("&Genre=动作"); sb.Append("&Price=45.5"); byte[] b = new UTF8Encoding().GetBytes(sb.ToString()); request.ContentLength = b.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(b, 0, b.Length); } try { //响应 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); int statusInt = response.StatusCode.GetHashCode(); //相应成功 if (response.StatusCode.ToString() == "OK") { string responseUri = response.ResponseUri.AbsoluteUri; MessageBox.Show("修改成功!"); LoadPre(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值