第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

慕尼黑8549860 2023-07-19 17:07:39
我正在嘗試連接“ https://api.tdax.com/api/orders/?pair=btc_thb ” 這個網(wǎng)址在 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 代碼無法運行,出現(xiàn) 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貢獻(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è)置它。


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

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

}


查看完整回答
反對 回復(fù) 2023-07-19
  • 2 回答
  • 0 關(guān)注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號