简体   繁体   中英

Before I can get JSON file, how can I build correct connection with getting 503 Server Error

I was trying several days to get HTTPS connection right to connect to Yobit public API. I don't know what happen to my code. I have tried so many different examples but nothing works out on Yobit. Those codes and examples I have tried, they either give 411, 503 error or MalFormException:no protocol. Can anyone help me? I have very limited experience with HTTPS or web programming on Java. If any one can provide me solutions and references, I will really appreciate that.

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

}

Try to use " https://www.yobit.net/api/3/info " URL Instead of " https://yobit.net/api/3/info " It will give you the same result. You can validate it from the browser Window.

Check below snippet.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM