2 回答

TA貢獻1784條經驗 獲得超7個贊
某些服務器期望User-Agent
請求中存在標頭,以將其視為有效請求。因此,您需要將其添加到您的請求中,如下所示。
con.setRequestProperty("User-Agent", "My-User-Agent"); int responseCode = con.getResponseCode();
該標頭的值(My-User-Agent
在上面的示例中)可以設置為此端點所需的任何字符串。例如,PostmanPostmanRuntime/7.16.3
為此設置了類似的內容。
C# 可能會在內部執行此操作,因此您不必顯式設置它。

TA貢獻1775條經驗 獲得超8個贊
public String getOrders(SatangCurrencyPairs currencyPair) throws IOException, BadResponseException {
String operation="orders/?pair="+currencyPair.toString();
StringBuilder result = new StringBuilder();
URL url = new URL(baseUrl+operation);
//URL url_ = new URL("https://api.tdax.com/api/orders/?pair=btc_thb");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "java client");
con.setRequestMethod("GET");
//https://api.tdax.com/api/orders/?pair=btc_thb
int responseCode=con.getResponseCode();
if(responseCode!=HttpURLConnection.HTTP_OK){
throw new BadResponseException(responseCode);
}
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
return result.toString();
}
添加回答
舉報