部署至测试服务器后Web API GET请求报文件找不到错误求助
问题与解决方案
问题
我开发的调用JSON GET接口的程序在本地Visual Studio运行正常,但部署到测试服务器后报错,怀疑是服务器权限限制导致,求解决办法。
错误信息
An error has occurred via the CCR Referrals application. See the exception details below for further explanation.
异常详情:
System.Net.WebException: 找不到文件 'c:\windows\system32\inetsrv\mxy=445544'。 ---> System.Net.WebException: 找不到文件 'c:\windows\system32\inetsrv\mxy=445544'。 ---> System.IO.FileNotFoundException: 找不到文件 'c:\windows\system32\inetsrv\mxy=445544'。 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) 在 System.Net.FileWebStream..ctor(FileWebRequest request, String path, FileMode mode, FileAccess access, FileShare sharing, Int32 length, Boolean async) 在 System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean asyncHint) --- 内部异常堆栈跟踪的结尾 --- 在 System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean asyncHint) 在 System.Net.FileWebRequest.GetResponseCallback(Object state) --- 内部异常堆栈跟踪的结尾 --- 在 System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest&
相关代码
string API_URL = (string)System.Configuration.ConfigurationManager.AppSettings["API_URL"]; string username = "TheDudeAbides"; string password = "xyz1234"; string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)); //拼接完整API URL和要查询的rn API_URL = API_URL + "rn="+rn; try { //从URL调用结果中获取JSON数据 using (WebClient client2 = new WebClient()) { //设置基础认证并添加凭据 client2.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials; var responseOK = client2.DownloadString(PRES_API_URL); List<Protocol> listProts = Service.GetOpenProtsAppTable(); if (responseOK != null) { if (responseOK != "") { JArray jsonArray = JArray.Parse(responseOK); foreach(JObject item in jsonArray) { Patient_Enrollment pe = new Patient_Enrollment(); //从JSON响应中找到协议ID var itemProperties = item.Children<JProperty>(); var protocolElement = itemProperties.FirstOrDefault(x => x.Name == "protocol"); var protocol = protocolElement.Value; Protocol prot = listProts.FirstOrDefault(i => i.protocolId_Display == protocol.ToString()); if (prot != null) { pe.IprotocolId = prot.protocolId.ToString().Replace("-", ""); pe.IshortTitle = prot.shortTitle; pe.IstudyId = Convert.ToString(prot.studyId); //将API数据转为Patient_Enrollment对象,用于对比Referrals系统中的患者入组信息 enrollments.Add(pe); } } } } } } catch (WebException e) { //发送API故障通知邮件 Email.SendErrorNotification("API Down", e.ToString()); }
解决办法
检查URL配置与变量一致性
- 确认配置文件中
API_URL是完整的HTTP/HTTPS开头的绝对地址(如https://your-api-domain.com/api/),而非相对路径或不完整地址。 - 代码中拼接的是
API_URL,但调用DownloadString用的是PRES_API_URL,需确保这两个变量指向同一有效地址,不存在未赋值或变量名错误的情况。
- 确认配置文件中
强制验证URL协议
在调用DownloadString前,显式校验URL是否为合法的HTTP/HTTPS地址,避免WebClient将错误路径解析为本地文件:using (WebClient client2 = new WebClient()) { Uri apiUri; if (!Uri.TryCreate(PRES_API_URL, UriKind.Absolute, out apiUri) || (apiUri.Scheme != Uri.UriSchemeHttp && apiUri.Scheme != Uri.UriSchemeHttps)) { throw new InvalidOperationException("API地址格式错误,必须是HTTP/HTTPS绝对路径"); } //后续认证及请求逻辑 client2.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials; var responseOK = client2.DownloadString(apiUri); //... }排查应用程序池权限(次要)
若URL确认无误但仍有问题,检查测试服务器上该应用的应用程序池身份:- 确保账号有访问目标API的网络权限(如内网API的访问权限、外部API的出站权限)。
- 若服务器需通过代理访问外部API,需为WebClient配置代理:
client2.Proxy = new WebProxy("http://your-proxy-server:port", true);
内容的提问来源于stack exchange,提问作者James Waters
相关产品推荐
相关产品推荐

