【问题标题】:when calling Api, working fine on local while not working when hosted in C#调用 Api 时,在本地工作正常,而在 C# 中托管时不工作
【发布时间】:2021-11-11 13:50:51
【问题描述】:

System.Net.WebException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应。”

我使用了以下代码:

string ClaimStatus_url = "https://qms.futuregenerali.in/FGClaimWsAPI/api/Common/FetchClaimdetails";

var httpWebRequest = (HttpWebRequest)WebRequest.Create(ClaimStatus_url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.Accept = "application/json";
httpWebRequest.KeepAlive = true;
httpWebRequest.Timeout = -1;

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    string json = "{\"ClaimNo\":\"" + userProfile.ClaimNumber + "\"}";
    //string json = "{\"ClaimNo\":\"CV010831\"}";
    streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
var result1 = "";
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
    result1 = result.Substring(1, result.Length - 2); // to bring response result in proper format
}

【问题讨论】:

  • 托管意味着您将代码部署在服务器上?如果是,服务器无法访问 api 或无法访问互联网。
  • 是的先生,我已经部署了代码
  • 听起来像是一个非公共 API,您可能需要 IP 或主机名白名单才能访问它。

标签: c# rest


【解决方案1】:

获取生成的 URL 并将其粘贴到本地计算机上的浏览器中。如果失败,则通过远程访问登录服务器并在远程会话中执行相同操作。

如果两者都失败,那么问题出在托管服务器上(防火墙端口未打开?)如果仅本地失败,则跟踪调用以找到确切的失败点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 2020-03-21
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多