Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpURLConnection GET call with parameters not working

I have a very simple code which is not working.

Class HttoCon:

package test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class HttpCon {


public static void main(String[] args) {
    try {
        sendGet();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    // HTTP GET request
        private static void sendGet() throws Exception {

            String url = "http://myweb.com/public/resource/data/stream";

            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();

            // optional default is GET
            con.setRequestMethod("GET");


            //add request header
            con.setRequestProperty("begin", "1430295300000");
            con.setRequestProperty("end", "1430297279988");
            con.setRequestProperty("id", "140621");
            con.setRequestProperty("time", "FIFTEEN_MINUTE");

            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'GET' request to URL : " + url);
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result
            System.out.println(response.toString());

        }
}

Error Trace:

{"fault":{"exceptionType":"MissingServletRequestParameterException","host":"12.205.101.123","cause":"Required Long parameter 'id' is not present","status":"400","query":"/public/resource/data/stream","stackTrace":"org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'id' is not present\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseMissingParameterException(AnnotationMethodHandlerAdapter.java:774)\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:509)\n\tat 

NOTE:

If I hit the url directly in browser it works fine.

http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE

UPDATE:

Using curl:

curl -X GET http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE

Above curl doesn't work and the exception is same -

curl -X GET 'http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE'

This curl works fine.

like image 340
My God Avatar asked Apr 29 '15 10:04

My God


2 Answers

You create a String containing the parameters, which then gets appended onto the URL. Create a URL Obj from this. For example :-

String this.url = "http://myweb.com/public/resource/data/stream";
this.url += "?begin=1430295300000&end=1430297279988&id=140621
    &time=FIFTEEN_MINUTE"; 
this.urlObj = new URL(this.url);

Then you create the connection as in your original example.

like image 160
Craig Parkinson Avatar answered Oct 02 '22 14:10

Craig Parkinson


openConnection will establish the connection and issue the GET. Subsequent settings of GET parameters on the returned URLConnection object are ignored, since the URL has already been open.

Add the parameters to the URL as query string params just as the link above and open that link

or

try to post the parameters to the server (which is even better)

like image 32
Axel Amthor Avatar answered Oct 02 '22 14:10

Axel Amthor