Running a URL in Advanced Rest Client from a java program -
i have url of api working fine if run in advanced rest client of chrome directly. want url trigger own rest api code should run in advanced rest client , stores result in variable. how can this?
use apache httpclient library https://hc.apache.org/ or other third party open source libraries easy coding. if using apache httpclient lib, please google sample code. tiny example here.
httpclient client = new defaulthttpclient(); httpget request = new httpget('http://site/myresturl'); httpresponse response = client.execute(request); bufferedreader rd = new bufferedreader (new inputstreamreader(response.getentity().getcontent())); string line = ''; while ((line = rd.readline()) != null) { system.out.println(line); } return (rd);
if there restriction use third party jars, can in plain java too.
httpurlconnection conn = null; try { url url = new url("http://site/myresturl"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setrequestproperty("accept", ""); // add content mime type if (conn.getresponsecode() != 200) { throw new runtimeexception("failed : http error code : " + conn.getresponsecode()); } bufferedreader br = new bufferedreader(new inputstreamreader( (conn.getinputstream()))); string output; while ((output = br.readline()) != null) { system.out.println(output); } conn.disconnect(); } catch (exception e) { e.printstacktrace(); }
Comments
Post a Comment