【问题标题】:calling a https api from another web api从另一个 Web api 调用 https api
【发布时间】:2019-02-28 19:11:11
【问题描述】:

我附上了以下代码,它在 localhost 中运行良好,但在另一台服务器上的 IIS 中托管时会引发 Web 异常/套接字。

System.Net.Sockets.SocketException (0x80004005):连接尝试失败,因为连接方一段时间后没有正确响应,或者连接失败,因为连接主机没有响应40.113.232.243:443

它在本地也抛出了同样的错误,除非我添加了这一行—— httpWebRequest.Proxy = WebRequest.GetSystemWebProxy();

但它在 iis 服务器中托管时会引发 socketexception。

public async Task<string> Get()
{
        try
        {
            string uri = "https://hp-reporting-*****.azurewebsites.net/********";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
            httpWebRequest.Timeout = 600000;

            httpWebRequest.Proxy = WebRequest.GetSystemWebProxy(); // adding this line resolved error in local but still same issue persists when hosted in iis in another server

            httpWebRequest.Method = "GET";

            HttpWebResponse httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();

            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var response = streamReader.ReadToEnd();
                // this is your code here...
                System.Xml.Linq.XNode node = JsonConvert.DeserializeXNode(response, "Root");

                return node.ToString();
            }

【问题讨论】:

    标签: asp.net-web-api proxy


    【解决方案1】:

    好吧,看看那行的作用:https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequest.getsystemwebproxy?view=netframework-4.7.2

    在您的本地计算机上,您在 Internet Explorer 中定义了一个 Web 代理,您在拨打电话时使用该代理。在部署的 IIS 上,您显然没有它。

    因此,您要么完全按照设置本地计算机的方式设置服务器,要么找到另一种在本地解决此问题的方法,而不使用该本地代理。当你让它工作时,你再次部署它就会工作。

    【讨论】:

    • 是的,你能建议一些方法吗,我检查了 bool isBypassed = httpWebRequest.Proxy.IsBypassed(exampleUri)...它返回 false,这意味着 url 使用代理.
    • 当解决方案托管在服务器上时,我需要有关如何访问此代理的帮助。
    • httpWebRequest.Proxy = WebRequest.GetSystemWebProxy(); httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";解决了我的问题。托管后成功调用了api。
    猜你喜欢
    • 1970-01-01
    • 2016-12-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多