简体   繁体   English

来自 .net C# 的 WebService 调用出现错误:(502) 网关错误

[英]WebService Call from .net C# getting error : (502) Bad Gateway

Trying to call WebServices from C# and getting below error:尝试从 C# 调用 WebServices 并出现以下错误:

System.Net.WebException: 'The remote server returned an error: (502) Bad Gateway

Code:代码:

WebRequest request = WebRequest.Create("https://xxxxx/cgi/webservice.pl?function=get_latest_ts_values&site_list=130105B&datasource=AT&varfrom=10.00&varto=10.00&lookback=60&format=csv");
        request.Method = "GET";
        WebResponse response = request.GetResponse();
        using (Stream dataStream = response.GetResponseStream() )
        {
            StreamReader reader = new StreamReader(dataStream);
            string responseFromServer = reader.ReadToEnd();
            Console.WriteLine(responseFromServer);
            Console.ReadLine();
        }

But works fine when i use Postman or just copy url in browser and also works fine with below python code:但是当我使用 Postman 或只是在浏览器中复制 url 并且在以下 python 代码中也可以正常工作:

import requests

dataload = {}
dataurl = "https://xxxxx/cgi/webservice.pl?function=get_latest_ts_values&site_list=130105B&datasource=AT&varfrom=10.00&varto=10.00&lookback=60"
headers = {}
response = requests.request("GET", dataurl, headers=headers, data=dataload)
for dataresp in response:
    print(dataresp)

What am I doing wrong with C# code? C# 代码我做错了什么?

The uri for the WebRequest has the query parameter &format=csv . WebRequest 的 uri 具有查询参数&format=csv Maybe this is why you are getting a 502. The Python request is missing that query parameter.也许这就是您得到 502 的原因。 Python 请求缺少该查询参数。 Did you try the WebRequest by removing that part?您是否通过删除该部分来尝试 WebRequest?

Could be incorrect content type or user agent having the wrong information.可能是不正确的内容类型或具有错误信息的用户代理。 Postman could be setting these values without your knowledge. Postman 可能在您不知情的情况下设置这些值。 Might try in the exception seeing if there is aa response stream and read it through a streamreader to see if there is any more information you're not seeing to point you in the correct direction.可能会尝试在异常中查看是否有响应 stream 并通过流式阅读器读取它,以查看是否有更多您没有看到的信息可以为您指明正确的方向。

Ended up using RestSharp and it works fine.最终使用 RestSharp 并且工作正常。 ( https://www.nuget.org/packages/RestSharp ) https://www.nuget.org/packages/RestSharp

 string Uri = "https://xxxx/cgi/webservice.pl?xxxx";
 var client = new RestSharp.RestClient(Uri);
 client.Timeout = -1;
 var request = new RestRequest(Method.GET);
 IRestResponse response = client.Execute(request);
 Console.WriteLine(response.Content);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 502坏网关 - 来自C#的POST但在fiddler中工作正常 - 502 bad gateway - POST from C# but works fine in fiddler 如何在ASP.NET MVC 5中调用SOAP Web服务时修复502 Bad Gateway错误? - How to fix 502 Bad Gateway error when calling SOAP webservice in ASP.NET MVC 5? 从另一个 locahost.Net Core API 调用 locahost.Net Core API 时出现错误网关(502)错误 - Bad Gateway (502) error while calling locahost .Net Core API from another locahost .Net Core API 从 .NET c# 调用本地 WebService - Call a local WebService from .NET c# 从WebService(.NET)用C#调用javascript - Call javascript with C# from WebService (.NET) HTTP 502坏网关c#与poloniex上的wamp - HTTP 502 bad gateway c# with wamp on poloniex ASP.NET Core 2.0 ngrok 502 错误网关错误 - ASP.NET Core 2.0 ngrok 502 Bad Gateway Error 从 Azure 中托管的 Asp.net Core MVC Web App 获取“响应状态代码并不表示成功:502(坏网关)” - Getting "Response status code does not indicate success: 502 (Bad Gateway)" from Asp.net Core MVC Web App hosted in Azure 502 Bad Gateway /在passwordigninasync调用失败 - 502 Bad Gateway / failing on passwordsigninasync call 如何解决 502 Bad Gateway 错误 - How to troubleshoot 502 Bad Gateway error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM