I have a question regarding using an HttpUrlConnection with a https web service.
Basically, we had previously written a bunch of web services to be used in an Android application that were all http calls. We want to change those over to use https instead. Basically, what I'm concerned with, is will my existing code work properly? I had previously created a connection like the following:
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();
myConnection.setConnectTimeout(TIMEOUT_MILLISEC);
myConnection.connect();
This appears to be working fine with the new https web services, but I'm wondering why I don't need to change it to something like this:
HttpsURLConnection myConnection = (HttpsURLConnection) myURL.openConnection();
myConnection.setConnectTimeout(TIMEOUT_MILLISEC);
myConnection.setSSLSocketFactory(createSSLSocketFactory());
myConnection.connect();
I'm still planning on moving to a HttpsURLConnection instead, but I'm curious how older versions of our app will be affected by our intended changes.
Thanks for the help!
In docs for HttpUrlConnection you will find:
Calling openConnection() on a URL with the "https" scheme will return
an HttpsURLConnection
But since HttpsUrlConnection extends from HttpUrlConnection, this code:
HttpURLConnection myConnection = (HttpURLConnection) myURL.openConnection();
will NOT result in class cast exception if url is for https connection. You still can cast to HttpsUrlConnection - just for safety you can check with instanceof if url scheme should change.
Related
I am trying to understand when to use URLConnection Class & when to use HttpURLConnection class. On doing some research, I came to know that URLConnection is used for nonHTTP connections & HttpURLConnection is used for specific HTTP connections. Can someone help me to know that what these nonHTTP connections refer to ?
PS - Please note that my qn is not regarding inheritance relationship of URLConnection Class & HttpURLConnection class.
It is regarding what do we mean by nonHTTP connections (that can be handled by URLConnection Class). Is nonHTTP connection means a datagram connection or ftp connection or something else.Refer below website:
https://www.experts-exchange.com/questions/21420009/URLConnection-and-HttpURLConnection.html
A URLConnection is an abstract class, representing any connection to an URL.
A HttpURLConnecton IS a URLConnection (subclass) and offers more methods.
That being said, you could open a URL with the HTTP protocol and assign the return value to a URLConnection, but you'd be missing the more specific methods.
You can open a URL to an ftp resource, or anything else and assign the result to a URLConnection, but it might not support the underlying protocol. For example, FTP support is very limited.
See the documentation for URL.openConnection():
If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned.
So, yes, use HttpURLConnection for HTTP connection and any specialized URLConnection you have, or use the raw URLConnection otherwise. That being said, it depends with which protocol you want to work - you might need a more complete client for any non-trivial protocol.
I need to create a simple HTTP client program with Java.
I've not found any example of implementation in Java that allows calling the OPTIONS method to get the Allow header with the allowed methods on the server.
I tried using:
HttpURLConnection http = (HttpURLConnection) url.openConnection();
System.out.println(http.getHeaderFields());
But the field Allow: GET, POST ... is not included.
The connection object fires a GET request by default. You need to set the request method to OPTIONS.
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
System.out.println(conn.getRequestMethod()); // GET
conn.setRequestMethod("OPTIONS");
System.out.println(conn.getHeaderField("Allow")); // depends
Can anybody tell me how to write a java client code to call restful web service with one parameter say email? I am trying the below code. But I am getting response as Success. Once this is success, I need the below XPHONE value. How to get this value?
XPHONE: 52-33-3669-7000
Here is the client code:
URL url = new URL("http://bluepages.ibm.com/BpHttpApisv3/wsapi?byInternetAddr=user.email");
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestProperty("Accept",
"application/json");
conn.connect();
I think you just missing response body handling.
There is nice article about rest-client code: article.
You can try using the JavaLite Http client:
JavaLite Http
Depends on how API is implemented value can be in response body or even in header, so this info you should know from specification or ask in dev team.
First try to check if everything works fine using CURL or better "Advanced rest client" ( its extension in Chrome browser) if it works, than just transfer flow to your code. How to use advanced rest client look here
I have a MVC application and I'd like that each request must handle its own session.
My request:
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
As we know CookieHandler allow just one session per application. I'd like that each request creates and use its own session.
Does anybody know how to do it?
Thanks
you will have to use Proxy while opening a connection...
the use of proxy provides always a new IP address for the server so you can be sure that the server maintains different session for each request...
your code will be something like following...
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection(new Proxy("some_proxy"));
How to set tcp.nodelay for below given code:
URL url = new URL(urlText);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
As far as I am aware you cannot set tcp.noDelay on `HttpURLConnection' as this does not allow any interface to alter underlaying tcp socket.
What I can recommend is try using Apache http client as it provide mechanism to set multiple TCP options. Have a look at this DefaultHttpClient