Eclipse Outbound connection blocked : The same url works from web browser -
i using httpurlconnection class connect external web service eclipse, getting error message "connection refused"
public class testconnection { static { //for localhost testing javax.net.ssl.httpsurlconnection.setdefaulthostnameverifier( new javax.net.ssl.hostnameverifier(){ public boolean verify(string hostname, javax.net.ssl.sslsession sslsession) { if (hostname.equals("localhost")) { return true; } return false; } }); } public static void main(string[] args) { httpurlconnection urlconnection; try { //https method required parameters // xmlhttprequest s1 = new xmlhttprequest (); urlconnection = (httpurlconnection) ((new url("https://google.com").openconnection())); urlconnection.setrequestmethod("get"); urlconnection.setrequestproperty("user-agent", ""); int https_response_code = urlconnection.getresponsecode(); bufferedreader in = new bufferedreader( new inputstreamreader(urlconnection.getinputstream())); string inputline; stringbuffer response = new stringbuffer(); while ((inputline = in.readline()) != null) { response.append(inputline); } in.close(); string https_result=response.tostring(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } } java.net.connectexception: connection refused: connect @ java.net.dualstackplainsocketimpl.connect0(native method) @ java.net.dualstackplainsocketimpl.socketconnect(unknown source) @ java.net.abstractplainsocketimpl.doconnect(unknown source) @ java.net.abstractplainsocketimpl.connecttoaddress(unknown source) @ java.net.abstractplainsocketimpl.connect(unknown source) @ java.net.plainsocketimpl.connect(unknown source) @ java.net.sockssocketimpl.connect(unknown source) @ java.net.socket.connect(unknown source) @ sun.security.ssl.sslsocketimpl.connect(unknown source) @ sun.security.ssl.basesslsocketimpl.connect(unknown source) @ sun.net.networkclient.doconnect(unknown source) @ sun.net.www.http.httpclient.openserver(unknown source) @ sun.net.www.http.httpclient.openserver(unknown source) @ sun.net.www.protocol.https.httpsclient.<init>(unknown source) @ sun.net.www.protocol.https.httpsclient.new(unknown source) @ sun.net.www.protocol.https.abstractdelegatehttpsurlconnection.getnewhttpclient(unknown source) @ sun.net.www.protocol.http.httpurlconnection.plainconnect0(unknown source) @ sun.net.www.protocol.http.httpurlconnection.plainconnect(unknown source) @ sun.net.www.protocol.https.abstractdelegatehttpsurlconnection.connect(unknown source) @ sun.net.www.protocol.http.httpurlconnection.getinputstream0(unknown source) @ sun.net.www.protocol.http.httpurlconnection.getinputstream(unknown source) @ java.net.httpurlconnection.getresponsecode(unknown source) @ sun.net.www.protocol.https.httpsurlconnectionimpl.getresponsecode(unknown source) @ testconnection.main(testconnection.java:19)
but if try connect same site browser able response service. can please let me know if there work around?
by default, httpurlconnection
class not allow localhost
hostname. need define custom hostname verifier allow localhost
. can place code static
block @ top of class intend use httpurlconnection
:
public final class yourclassname { static { //for localhost testing javax.net.ssl.httpsurlconnection.setdefaulthostnameverifier( new javax.net.ssl.hostnameverifier(){ public boolean verify(string hostname, javax.net.ssl.sslsession sslsession) { if (hostname.equals("localhost")) { return true; } return false; } }); } // use httpurlconnection here ... }
Comments
Post a Comment