我使用HTTPWebRequest
在C#中连接到一个.netStandard项目中的服务器。
码。
byte[] buffer = Encoding.ASCII.GetBytes(data);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// Send request
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
request.CookieContainer = cookies;
Stream postData = request.GetRequestStream();
postData.Write(buffer, 0, buffer.Length);
postData.Close();
// Get and return response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream Answer = response.GetResponseStream();
StreamReader answer = new StreamReader(Answer);
string ans = answer.ReadToEnd();
我使用的应用程序UWP的netstandard项目。该服务器是IIS。现在用我的私有IP从我的机器称为服务器(我的机器)时,此代码的工作。当我更改运行应用程序的机器,也是服务器(服务器再次是,在同一台计算机上的应用程序),我得到
System.Net.WebException:发送请求时发生错误。与此错误代码相关的文本无法被发现。与服务器的连接不能建立
我已检查该URL是正确的,当我做PostMan
后它的工作原理。我也已经启用了“关于UWP应用Private networks Client&Sever capabilities
”可能是什么问题呢?
编辑:
的内部异常是:“在发送所述请求时发生错误”
HRESULT:-2147012867
状态:不明错误
解决的办法是添加回送豁免的应用程序。因为网络隔离的,一些UWP应用被限制使用IP环回地址。
从this link的步骤8。
由于网络隔离,UWP应用程序,如应用程序安装程序被限制使用,IP回环地址一样http://localhost/。当使用本地IIS服务器,应用程序安装程序必须添加到回环排除列表中。
在我来说,我不得不添加环回免除我的应用程序。
CheckNetIsolation.exe LoopbackExempt -s
CheckNetIsolation.exe LoopbackExempt -d -n="<your package name>"