3

我尝试了几天来获得 HTTPS 连接以连接到 Yobit 公共 API。我不知道我的代码发生了什么。我尝试了很多不同的例子,但在 Yobit 上没有任何效果。我尝试过的那些代码和示例,它们要么给出 411、503 错误,要么给出 MalFormException:no 协议。谁能帮我?我在 Java 上的 HTTPS 或 Web 编程经验非常有限。如果有人可以为我提供解决方案和参考,我将不胜感激。

public void buildHttpsConnection() 
{
    try {
        URL url = new URL("https://yobit.net/api/3/info");
        HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("user-Agent", "Mozilla/5.0 (compatible; JAVA AWT)");
        con.setRequestProperty("Accept-Language","en-US,en;q=0.5");

        con.setDoOutput(true);
        con.setUseCaches(false);
        System.out.println(con.getResponseCode());
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

}
4

1 回答 1

2

尝试使用“ https://www.yobit.net/api/3/info ” URL 而不是“ https://yobit.net/api/3/info ” 它会给你同样的结果。您可以从浏览器窗口对其进行验证。

检查下面的片段。

 try {
                URL url = null;
                try {
                    url = new URL("https://www.yobit.net/api/3/info");
                } catch (MalformedURLException e1) {
                    e1.printStackTrace();
                }
                HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
                try {
                    con.setRequestMethod("GET");
                } catch (ProtocolException e1) {
                    e1.printStackTrace();
                }
                con.setRequestProperty("user-Agent", "Mozilla/5.0 (compatible; JAVA AWT)");
                con.setRequestProperty("Accept-Language","en-US,en;q=0.5");

                con.setDoOutput(true);
                con.setUseCaches(false);
                con.connect();

                try {
                    System.out.println(con.getResponseCode());
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
于 2018-02-07T12:45:10.057 回答