2 回答

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

TA貢獻(xiàn)1775條經(jīng)驗 獲得超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();
}
添加回答
舉報