我正在尝试使用下面的代码通过WebHttpRequest获取json字符串(它尝试调用一个文件,该文件将为我返回应用程序中的json )。它在我的机器上工作,但当我部署到用户服务器时,我得到了错误
“基础连接已关闭:发送时发生意外错误。”。
当我尝试为URL运行powershell invoke-webrequest时,我得到了相同的错误,但奇怪的是,当我使用Postman (get或post)作为URL时,无论是在用户服务器内部还是外部,它都工作得很好。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;
request.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
String jsonResponse = reader.ReadToEnd();
我还尝试使用另一种方法(WebClient)来获取网址,但它不能很好地工作。
String jsonResponse = null;
using (var webClient = new WebClient())
{
jsonResponse = webClient.DownloadString(url);
}
我在异常中得到的完整错误:
远程服务器返回错误:(401)未授权。System.Net.HttpWebRequest.GetResponse()上的系统
有谁知道吗?
谢谢。
发布于 2018-06-07 03:54:30
我想这可能是你的案子"The underlying connection was closed: An unexpected error occurred on a send." With SSL Certificate
看起来您需要设置ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
顺便说一句,你的代码中有:
request.UseDefaultCredentials = true; request.PreAuthenticate = true; request.Credentials = CredentialCache.DefaultCredentials;
那么什么是凭据呢?网络凭据是否与AD凭据类似?是否有权限访问该服务器?
https://stackoverflow.com/questions/50727502
复制相似问题