API调用在本地成功,但是在网站发布后失败,即使使用WebProxy后也是如此 [英] API call succeeds locally but fails after website is published, even after using WebProxy

查看:233
本文介绍了API调用在本地成功,但是在网站发布后失败,即使使用WebProxy后也是如此的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net MVC中有一个站点,该站点通过第三方API向用户发送SMS. 当我在本地测试时,API会按计划工作,但是当我上传到网络上时,会失败并出现以下错误.这是它引发的错误:

I have a site in asp.net MVC which sends SMS to users through a 3rd party API. The API works as planned when I test locally but fails with the following error once I upload onto the web. This is the error it throws:

连接尝试失败,因为被连接方未 一段时间后正确响应或建立连接 失败,因为连接的主机未能响应80.85.87.122:443"

"A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 80.85.87.122:443"

最初,我尝试使用HTTP GET访问API,然后使用XML切换到POST,后一种方法返回以下错误: 无法通过

Originally I was attempting to access the API using HTTP GET then switched to POST using XML, the latter approach returned the following error: Unable to connect to the remote server at

System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) 在System.Net.HttpWebRequest.GetRequestStream()

System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream()

我消耗资源的代码如下:

My code to consume the resource is below:

private async Task SendSmsUsingXML()
        {
            WebRequest req = null;
            WebResponse rsp = null;
            try
            {
                string fileName = Server.MapPath("~/App_Data/MultiTexterSMSFormat.xml");
                string uri = "https://www.multitexter.com/tools/xml/Sms.php";
                req = WebRequest.Create(uri);
                //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
                req.Method = "POST";        // Post method
                req.ContentType = "text/xml";     // content type
                //req.Proxy = new WebProxy("202.79.27.50", 8080);
                // Wrap the request stream with a text-based writer
                StreamWriter writer = new StreamWriter(req.GetRequestStream());
                // Write the XML text into the stream
                writer.WriteLine(this.GetTextFromXMLFile(fileName));
                writer.Close();
                // Send the data to the webserver
                rsp = req.GetResponse(); //I am getting error over here
                StreamReader sr = new StreamReader(rsp.GetResponseStream());
                string result = sr.ReadToEnd();
                sr.Close();
                Response.Write(result);

            }
            catch (WebException webEx)
            {
                Response.Write(webEx.Message.ToString());
                Response.Write(webEx.StackTrace.ToString());
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
                Response.Write(ex.StackTrace.ToString());
            }
            finally
            {
                if (req != null) req.GetRequestStream().Close();
                if (rsp != null) rsp.GetResponseStream().Close();
            }
        }

同样,此操作在本地有效,但是一旦我发布到网络上,便失败了. 然后,我联系了我的房东(www.1and1.com),因为所有手指似乎都在指出它们是问题所在. 他们的客户服务告知我,由于我的托管程序包(共享的托管程序包)的限制,此请求被阻止,如果我升级到专用的托管程序包不再是问题.服务器软件包. 对于我来说,这是没有意义的,因为即使是最基本的网站,简单的API调用也是必不可少的. 经过更多的挖掘之后,我发现了帖子,其中用户抱怨与同一托管公司存在相同问题.他的解决方案是使用代理服务器,导致1and1接受请求. 我上网,寻找了免费的代理服务器和端口用于测试,我找到了一个在本地工作的代理服务器和端口,将其上载到了实时站点,但仍然失败,并出现以下错误:

Again, this worked locally but failed once I published unto the web. I then contacted my host(www.1and1.com) since all fingers seemed to point at them being the problem. Their customer care informed me that this request was being blocked as a result of the limitations of my hosting package(which is a shared hosting package) and it would no longer be an issue if I upgraded to a dedicated server package. This made no sense to me as a simple API call is part and parcel of even the most basic sites. After doing more digging I found a post where a user complained of the same issue with the same hosting company. His solution was to use a proxy server, leading to 1and1 accepting the request. I went online, looked for a free proxy servers and ports for testing purposes, I located one that worked locally, uploaded it unto the live site and it still failed with this error:

无法连接到位于的远程服务器 System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) 在System.Net.HttpWebRequest.GetRequestStream()

Unable to connect to the remote server at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream()

在这一点上,我真的很乐意,并衷心感谢您为解决此问题提供的帮助和/或建议. 作为进一步参考,这是链接到SMS API I的文档正在尝试消费. 预先感谢.

I really am at my wits end at this point and would genuinely appreciate any help and/or recommendations on solving this. For further reference, this is the link to the documentation of the SMS API I am attempting to consume. Thanks in advance.

推荐答案

我建议在云中创建一个虚拟机并将其托管在其中.我在GoDaddy上遇到了同样的问题,所以我转到了Azure虚拟机.

I would suggest to create a virtual machine in the cloud and host it there. I had the same issue with GoDaddy so I moved to Azure virtual machine.

这篇关于API调用在本地成功,但是在网站发布后失败,即使使用WebProxy后也是如此的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆