简体   繁体   中英

HTTP GET request get HTML page code instead of expected json Response

/ Here is the code /

<html><head><meta http-equiv="Refresh" content="0; URL=http://www.fifacoinservice.com/api/v1"></head><body></body></html>
HTTP/1.1 400 Bad Request
Server: PipeGuard
Date: Mon, 15 Jun 2015 10:29:32 GMT
Content-Type: text/html
Content-Length: 605
Connection: close

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<h1>400 Bad Request</h1>
<p>Your browser sent a request that this server could not understand. Sorry for the inconvenience.<br/>
    Please report this message and include the following information to us.<br/>
    Thank you very much!</p>
<table>
    <tr>
        <td>URL:</td>
        <td>http://_</td>
    </tr>
    <tr>
        <td>Server:</td>
        <td>5.254.102.236.r00t-services.net</td>
    </tr>
    <tr>
        <td>Date:</td>
        <td>2015/06/15 10:29:32</td>
    </tr>
</table>
<hr/>Powered by PipeGuard</body>
</html>

Here is my call

URL url = new URL("http://www.server.com/api/v1");
            String query = "api_key=q5EicSKs027cjut7qhZk&method";

            //make connection
            URLConnection urlc = url.openConnection();

            //use post mode
            urlc.setDoOutput(true);
            urlc.setAllowUserInteraction(false);

            //send query
            PrintStream ps = new PrintStream(urlc.getOutputStream());
            ps.print(query);
            ps.close();

            //get result
            BufferedReader br = new BufferedReader(new InputStreamReader(urlc
                .getInputStream()));
            String l = null;
            while ((l=br.readLine())!=null) {
                System.out.println(l);
            }
            br.close();

You need to set Accept header.

urlc.addRequestProperty("Accept","application/json");

Update

Your server may not support application/json datatype. You can check it by curl .

curl -X POST  -H "Accept: application/json" --data 'api_key=q5EicSKs027cjut7qhZk&method' http://www.server.com/api/v1

Can you try something like below using Apache HTTP client ?

        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost("http://www.server.com/api/v1");

        post.setHeader("api_key", "q5EicSKs027cjut7qhZk&method");

        HttpResponse response = client.execute(post);

        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        JSONObject o = new JSONObject(result.toString());

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