When HTTPConnection is performed and what is its status - java

I have below code which generate OutputStream from HttpURLConnection
When connection is actually performed and how can I check its status?
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
httpCon.addRequestProperty("X-Auth-Token", getAuthToken());
httpCon.setDoInput(true);
httpCon.setRequestProperty("Connection", "close");
httpCon.setReadTimeout(READ_TIMEOUT);
httpCon.setRequestProperty("Transfer-Encoding","chunked");
httpCon.setDoOutput(true);
httpCon.setChunkedStreamingMode(STREAMING_CHUNK);
mOutputStream = httpCon.getOutputStream();

The underlying TCP connection is created (or allocated from a connection pool) when you get one of the streams or the response code, or call connect(). The HttpURLConnection object itself isn't a TCP connection.

Related

Authorization for Spotify API in Java HttpURLConnection

I'm relatively new to using http and APIs but I was trying to use HttpURLConnection in Java to connect to the Spotify API. I managed to get a GET to work but I can't figure out how to make the authorization work in order to access other materials. Here's my code, can anyone see what I'm doing wrong? It's returning a 400 response code.
URL url = new URL("https://accounts.spotify.com/api/token?grant_type=client_credentials");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Authorization", Base64.getEncoder().encodeToString("Basic bdfc603f24c54078a7365d3af39c2aed:<ClientSecret>".getBytes()));
You're missing the Content-Type, see my example.
URL url = new URL("https://accounts.spotify.com/api/token?grant_type=client_credentials");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
conn.setRequestProperty("Authorization", "Basic MDg4ZDc=");
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

HttpUrlConnection PATCH request using Java

I am trying to do a http PATCH request but I always get the 404 error, so maybe the settings of my connection are not correct:
URL url = new URL("MyPath");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("X-HTTP-Method-Override", "PATCH");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");
JsonObject jo = createMyJson();
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(jo.toString());
out.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());
I get the 404 error, Not found. When doing the same request using Postman, this is working..
Thank you for your help.
Not all servers support X-HTTP-Method-Override. In that case your last resort is (if you are not using a decent HTTP client) to hack the URLConnection object.
I posted a complete solution here on SO, check it out.

Failing to connect to a web server with Java

I got a strange task: to connect to webserver using Java even though I'm a php programmer. I used to program Java back at the university but it's been good few years since.
I took a code sample from stack-overflow. It runs fine, no compilation errors no exceptions, but, I don't see any outgoing connection when I sniff with Fiddler, and my server side script doesn't see any incoming connections either.
Any ideas of what can be wrong? Or, how do I debug this situation?
String urlParameters = "param1=a&param2=b&param3=c";
String request = "http://example.com/index.php";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setUseCaches (false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream ());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
connection.disconnect();

Blocking operations in URLConnection

Running the following code snippet:
URL url = new URL(uri);
URLConnection urlconn = url.openConnection();
urlconn.connect();
urlconn.getContentLength();
I get the dead-lock sometimes, usually at two lines: urlconn.connect() and urlconn.getContentLength(). Also, when I do
InputStream is = urlconn.getInputStream();
// doing some reading
is.close();
I also get blocking at is.close(). I want to ask why such blocking happens. It is clueless to me from the Java API.

open url connection and wait to finish loading of the url java

i have the following code. i want to get the url after finished loading the url. How can i do my connection to wait to finish the loading first?
Thank you in advance
URL u = new URL("http://google.com/search?sourceid=navclient&btnI=1&q=" + jTextField1.getText().toString());
HttpURLConnection con = (HttpURLConnection) u.openConnection();
HttpURLConnection ex1 = (HttpURLConnection) u.openConnection();
ex1.setChunkedStreamingMode(7000); // enable chunked streaming mode before connecting to server.

Categories

Resources