亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C#可以連接,但是java無法連接

C#可以連接,但是java無法連接

慕尼黑8549860 2023-07-19 17:07:39
我正在嘗試連接“ https://api.tdax.com/api/orders/?pair=btc_thb ” 這個網址在 chrome、郵遞員上工作。我可以用 C# 連接這個 url。但無法連接java。namespace Exchanges.Satang{    class SatangApi    {        private static class WebApi        {            private static readonly HttpClient st_client = new HttpClient();            static WebApi()            {                st_client.Timeout = TimeSpan.FromSeconds(2);            }            public static HttpClient Client { get { return st_client; } }            public static string Query(string url)            {                var resultString = Client.GetStringAsync(url).Result;                return resultString;            }        }        public static string GetOrders(string symbol)        {            const string queryStr = "https://api.tdax.com/api/orders/?pair=";            var response = WebApi.Query(queryStr + symbol);            return response.ToString();        }    }}此 C# 代碼運行良好,但以下 Java 代碼無法運行,出現 403 錯誤。    private String publicOperation(String operation) throws IOException, BadResponseException {        StringBuilder result = new StringBuilder();        URL url = new URL(baseUrl+operation);        HttpURLConnection con = (HttpURLConnection) url.openConnection();        //con.setRequestProperty("Content-Type", "application/json");        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();    }
查看完整描述

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# 可能會在內部執行此操作,因此您不必顯式設置它。


查看完整回答
反對 回復 2023-07-19
?
www說

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();

}


查看完整回答
反對 回復 2023-07-19
  • 2 回答
  • 0 關注
  • 178 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號