C#可以连接,但是java无法连接

我正在尝试连接“ 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();

    }


慕尼黑8549860
浏览 125回答 2
2回答

噜噜哒

某些服务器期望User-Agent请求中存在标头,以将其视为有效请求。因此,您需要将其添加到您的请求中,如下所示。con.setRequestProperty("User-Agent", "My-User-Agent"); int responseCode = con.getResponseCode();该标头的值(My-User-Agent在上面的示例中)可以设置为此端点所需的任何字符串。例如,PostmanPostmanRuntime/7.16.3为此设置了类似的内容。C# 可能会在内部执行此操作,因此您不必显式设置它。

www说

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();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java