HttpURLConnection“发生意外错误。”

时间:2017-01-09 05:50:37

标签: java web-services http

根据我对500错误的一点知识,我理解这是服务器错误。但是这背后的原因可能是什么呢?它可能在我的最后吗?

我得到的错误是:

{"status":500,"error":"An unexpected error occurred."}

它可能与我的标题有关,即缺少一个?从我测试中发现的错误变化400错误,即添加用户代理标题后的401。

我的代码如下:

String url="https://api.gotinder.com/auth";
    URL object=new URL(url);
    HttpURLConnection con = (HttpURLConnection) object.openConnection();

    con.setDoOutput(true);
    con.setDoInput(true);

    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Host", "host url");
    //con.setRequestProperty("content-Length" , "287");
    con.setRequestProperty("User-Agent" , "Tinder/4.0.4");
    con.setRequestProperty("facebook_token", "token");
    //con.setRequestProperty("facebook_id", "id");

    System.out.println(con.getResponseCode());

旁注:这都是出于教育目的。我很好奇。

1 个答案:

答案 0 :(得分:1)

问题是我将我的令牌作为财产传递而不是身体的一部分。

代码:

String urlstr = "https://api.gotinder.com/auth";
    String params = "facebook_token=" + this.fb_token;
    URL url = new URL(urlstr);
    HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
    urlconn.setDoInput(true);
    urlconn.setDoOutput(true);
    urlconn.setRequestMethod("POST");
    urlconn.setRequestProperty("User-Agent", "Tinder/3.0.4 (iPhone; iOS 7.1; Scale/2.00)");
    urlconn.setRequestProperty("Content-Language", "en-US");
    OutputStream os = urlconn.getOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
    writer.write(params);
    writer.close();
    os.close();

    if (urlconn.getResponseCode() == 200) {
        BufferedReader bR = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
        String line = "";

        StringBuilder responseStrBuilder = new StringBuilder();
        while ((line = bR.readLine()) != null) {
            responseStrBuilder.append(line);
        }
        urlconn.getInputStream().close();

        JSONObject result = new JSONObject(responseStrBuilder.toString());
        user_token = result.getString("token");
        System.out.println("User token is: " + user_token);

    } else {
        System.out.println("Want to print error here had getting data...");
    }