c# - HttpWebRequest 适用于 windows 窗体但不适用于 Web API
问题描述
我需要从 WEB API 方法调用 HTTPS POST 服务。我正在使用 HttpWebRequest 调用此 API,但是我收到错误消息“连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接的主机未能响应 xxxx:443”
当我尝试在 Windows 窗体中调用此 API 时,它运行良好。
HttpWebRequest r = null;
HttpWebResponse rsp = null;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
r = (HttpWebRequest)System.Net.WebRequest.Create("url");
String d = "Serialize JSON"
r.Method = "POST";
r.ContentType = "application/json";
StreamWriter wr = new StreamWriter(r.GetRequestStream());
wr.WriteLine(d);
wr.Close();
rsp = (HttpWebResponse)r.GetResponse();
StreamReader reader = new StreamReader(rsp.GetResponseStream());
String dd = reader.ReadToEnd();
解决方案
可能是因为您正在尝试使用 SSL,从最后一部分 - xxxx:443。我的理解是 443 用于 SSL。如果我不正确,很高兴有人纠正我。
看看以下内容,这可能会对您有所帮助:
https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/working-with-ssl-in-web-api
推荐阅读
- sql - 查找数据集中每年可用的每个国家/地区的前 2 个供应商?- 大查询 SQL
- android - vivo杀死应用程序时不接收广播
- kubernetes - Tekton 仪表板部署错误-来自服务器的错误(禁止):创建“config/service.yaml”时出错
- android - Flutter 使用 Cloud Firestore 和 Firebase Auth 存储用户数据
- visual-studio-code - 如何在 VS Code 1.57 中禁用工作区信任?
- r - 如何使用 lapply 遍历 ggplot2 中的 2 个变量
- java - 在泛型中向下转换对象,但在其他任何地方都没有?
- docker - rust docker 映像中的 .cargo 文件夹在哪里?
- php - get() 上的非静态方法调用方法
- python - 仅根据特定键/值查找重复项