java - HttpURLConnection GET call with parameters not working -
i have simple code 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 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 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 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' not present","status":"400","query":"/public/resource/data/stream","stacktrace":"org.springframework.web.bind.missingservletrequestparameterexception: required long parameter 'id' 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 hit url directly in browser works fine.
update:
using curl:
curl -x http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=fifteen_minute
above curl doesn't work , exception same -
curl -x 'http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=fifteen_minute'
this curl works fine.
you create string containing parameters, gets appended onto url. create url obj this. 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 create connection in original example.
Comments
Post a Comment