httpconnection - Java HttpUrlConnection throws Connection Refused -
i know there several question regarding topic did't find answer in of them.
i'm trying open connection local server keep getting connection refused.
i have server running , tested connection browser , google app called postman , works.
it's failing when opening connection if there nothing connect to. or maybe blocking connection? tested firewall , antivirus down, no luck.
testing in postman url returns user should...
if replace url "http://www.google.com" works fine.
here code:
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; /** * * @author gabriel */ public class httpconnection { public httpconnection() { } public void makerequest() throws malformedurlexception, ioexception { string url = "http://localhost:8000/users/1"; url obj = new url(url); httpurlconnection con = (httpurlconnection) obj.openconnection(); // optional default con.setrequestmethod("get"); //add request header con.setrequestproperty("user-agent", "mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/37.0.2062.120 safari/537.36"); con.setrequestproperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); con.setrequestproperty("accept-encoding", "gzip,deflate,sdch"); con.setrequestproperty("accept-language", "en-us,en;q=0.8,es;q=0.6"); con.setrequestproperty("connection", "keep-alive"); con.setrequestproperty("host", "localhost:8000"); 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()); } }
i have similar code working, request header lot simpler. just:
con.setrequestproperty("user-agent", "mozilla/5.0");
if simplifying header not help, capture traffic when using browser fiddler , making request that.
Comments
Post a Comment