簡體   English   中英

RESTful API請求可在Python和瀏覽器中使用,但不能在VB.Net中使用

[英]RESTful API request works in Python and browser but not in VB.Net

我想通過以下調用來調用RESTful API

https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D..EUR.SP00.A?updatedAfter=2018-08-01T07%3A05%3A02%2B07%3A05&startPeriod=2018-08-01&detail=dataonly&dimensionAtObservation=TIME_PERIOD

結果應為包含貨幣匯率的XML。

我需要讓它在vb.net中工作,但是(現在)不起作用了。 幾天前它仍然有效。 我使用以下代碼:

Try
    ' define webclient settings
    Dim _webClient As System.Net.WebClient = New System.Net.WebClient

    ' I tried with and without the following line
    ' _webClient.Headers(HttpRequestHeader.UserAgent) = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.90 Safari/537.36"

    Dim link as String = "https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D..EUR.SP00.A?updatedAfter=2018-08-01T07%3A05%3A02%2B07%3A05&startPeriod=2018-08-01&detail=dataonly&dimensionAtObservation=TIME_PERIOD"
    Dim sourceString As String = _webClient.DownloadString(link)

Catch ex As Exception
    ' Error in request url or on the server side
    Console.WriteLine(DateTime.Now.ToString("MMM dd hh:mm:ss") & " Error: " & ex.Message)

然后, ex.message包含以下信息:

遠程服務器返回錯誤:(500)內部服務器錯誤

但是,當復制並粘貼到地址欄和Python中時,相同的請求在我的瀏覽器中也有效。 我使用以下Python代碼:

import requests

url = 'https://sdw-wsrest.ecb.europa.eu/service/data/EXR/D..EUR.SP00.A?updatedAfter=2018-08-01T07%3A05%3A02%2B07%3A05&startPeriod=2018-08-01&detail=dataonly&dimensionAtObservation=TIME_PERIOD'

response = requests.get(url)
print(response.status_code) #returns 200

VB.Net代碼中有什么錯誤? 有什么區別?

我知道錯誤是什么。 我必須將Accept標頭設置如下:

_webClient.Headers(HttpRequestHeader.Accept) = "*/*"

然后就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM