远程服务器返回错误: (502) 错误的网关
与第三方供应商做接口的时候出现这个错误提示,下面是解决问题的详细过程。
开发环境:windows2012 vs2017 .net framework4.0 C#
接口环境:rest接口 https只支持tls1.2
本地开发运行通过,部署服务器发生错误
发现问题
远程服务器返回错误: (502) 错误的网关
定位问题
首先通过输出详细错误信息确认问题 来源stackoverflow
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
WebResponse resp = e.Response;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
throw new Exception(sr.ReadToEnd());
}
}
}
执行后,程序返回了一段html信息
<HTML><HEAD>
<TITLE>错误消息</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<BODY><TABLE><TR><TD id=L_dt_1><B>网络访问消息: 无法显示此页<B>
</TR></TABLE><TABLE><TR><TD height=15></TD></TR></TABLE><TABLE><TR>
<TD id=L_dt_2>技术信息(提供给支持人员) <UL>
<LI id=L_dt_3>错误代码: 502 Proxy Error。找不到主机。(11001)
<LI id=L_dt_4>IP 地址: 172.2.2.123
<LI id=L_dt_5>日期: 2021/2/24 6:24:43 [GMT]
<LI id=L_dt_6>服务器: VTMG01.YXXX.COM
<LI id=L_dt_7>源: 代理
</UL></TD></TR></TABLE></BODY>
</HTML>
提取关键信息后,得知访问目标服务器设置了代理,此时代理服务器访问目标服务器又不正常,下面要做的应该去掉代理,直接访问。
查找官网关于 httprequest proxy的资料 microsoft DefaultWebProxy
The DefaultWebProxy property reads proxy settings from the app.config file. If there is no config file, the current user's Internet Explorer (IE) proxy settings are used.
If the DefaultWebProxy property is set to null, all subsequent instances of the WebRequest class created by the Create or CreateDefault methods do not have a proxy.
System.Net.WebRequest.DefaultWebProxy属性 从app.config中读取代理设置,如果没有配置文件,则读取IE的代理设置。
如果该属性设置为NULL,则Create或CreateDefault创建的实例则不使用代理。
知道的错误的原因后,问题就迎刃而解了。
解决方案
多种解决方法,按使用场景选择。
1、Internent 选项→连接选项卡 →LAN设置 去掉代理设置
2、代码中设置代理 Create前设置代理为空 WebRequest.DefaultWebProxy = null
或创建对象后设置代理为空 request.Proxy=null
3、app.config中设置代理
方法二代码:
public static string HttpRequest(string url, string content)
{
try
{
WebRequest.DefaultWebProxy = null;//方法①
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
//request.Proxy=null; //方法②
request.Accept = "text/html,application/xhtml+xml,*/*";
request.ContentType = "application/json; charset=utf-8";
request.Method = "POST";
byte[] buffer = encoding.GetBytes(content);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
WebResponse resp = e.Response;
using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
{
throw new Exception(sr.ReadToEnd());
}
}
}
return "";
}
方法三 app.config
<system.net>
<defaultProxy>
<proxy usesystemdefault="false"/>
</defaultProxy>
</system.net>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?