HTTP OPTIONS method with Java - java

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

Related

Is there any way to integrate coinbase with java?

I was using below code to get the response but I Was getting the 403 error
URL url = new URL ("https://api.commerce.coinbase.com/checkouts");
Map map=new HashMap();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
From https://commerce.coinbase.com/docs/api/
Most requests to the Commerce API must be authenticated with an API
key. You can create an API key in your Settings page after creating a
Coinbase Commerce account.
You would need to provide minimal set of information to API in order for it to respond back with success code 200.
Yes, but it looks like you aren't providing enough information. There are two header fields that need to be supplied as well. These are X-CC-Api-Key which is your API key and X-CC-Version. See the link below.
https://commerce.coinbase.com/docs/api/#introduction
Header fields can be provided to HttpURLConnection using the addRequestProperty
https://docs.oracle.com/javase/8/docs/api/java/net/URLConnection.html#addRequestProperty-java.lang.String-java.lang.String-
URL url = new URL("https://api.commerce.coinbase.com/checkouts");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.addRequestProperty("X-CC-Api-Key", "YourSuperFancyAPIKey");
connection.addRequestProperty("X-CC-Version", "2018-03-22");
connection.setDoOutput(true);
You also want to be careful about what method you use. You are supplying a POST method in your example. This probably not what you want to start with. If you send a GET method you will receive back a list of all checks. This will be a good place to start.
https://commerce.coinbase.com/docs/api/#checkouts
GET to retrieve a list of checkouts
POST to create a new checkout
PUT to update a checkout
DELETE to delete a checkout
This type of API is known as REST.

JAVA POST request and then redirect to it?

What I need to do is send POST request to specific URL with two parameters and when the request is sent, I need to redirect user to that link so that he would be able to access functionality.
So far, what I have managed to do from various examples is this:
private void postRemoteAdvisoryLink() throws IOException {
URL obj = new URL(KdrmApplicationContext.getRemoteAdvisoryUrlPath());
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setConnectTimeout(60000);
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// For post only - start
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(("?auth=ssor&TransportKey=" + ssorTransportKey).getBytes());
os.flush();
os.close();
int responseCode = con.getResponseCode();
}
The problem is that now I get connection time out when trying to execute OutputStream os = con.getOutputStream(); line. Also, I still have no idea how to redirect user when request is completed.
Any ideas?
Using the basic Java URL classes would require you to manually handle the details of HTTP protocol - it's better to use libraries like Apache Http Components, as they deal with the underlying protocols for you. Some examples including POST requests can be found on their website.
Given the original question, the Timeout is likely related to host not responding or your Java application being unable to connect to given URL (due to no proxy configuration for example).
If you want to redirect a request based on the answer, you need to check the response headers and http status - if the status is 302, then there should be a header called Location, which will contain the URL you should make another request to.
Before getting an OutputStream, also make sure to set the Content-Length header (and ideally the Content-Type header as well).

Https Website using HttpURLConnection

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.

Java Client code to call restful web service

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

how to set tcp.nodelay when i use httpurlconnection

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

Categories

Resources