[英]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.