HttpURLConnection GET call with parameters not working - java

I have a very simple code which is not working.
Class HttoCon:
package test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class HttpCon {
public static void main(String[] args) {
try {
sendGet();
} catch (Exception e) {
e.printStackTrace();
}
}
// HTTP GET request
private static void sendGet() throws Exception {
String url = "http://myweb.com/public/resource/data/stream";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("begin", "1430295300000");
con.setRequestProperty("end", "1430297279988");
con.setRequestProperty("id", "140621");
con.setRequestProperty("time", "FIFTEEN_MINUTE");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to 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());
}
}
Error Trace:
{"fault":{"exceptionType":"MissingServletRequestParameterException","host":"12.205.101.123","cause":"Required Long parameter 'id' is not present","status":"400","query":"/public/resource/data/stream","stackTrace":"org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter 'id' is not present\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.raiseMissingParameterException(AnnotationMethodHandlerAdapter.java:774)\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestParam(HandlerMethodInvoker.java:509)\n\tat
NOTE:
If I hit the url directly in browser it works fine.
http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE
UPDATE:
Using curl:
curl -X GET http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE
Above curl doesn't work and the exception is same -
curl -X GET 'http://myweb.com/public/resource/data/stream?begin=1430295300000&end=1430297279988&id=140621&time=FIFTEEN_MINUTE'
This curl works fine.

You create a String containing the parameters, which then gets appended onto the URL. Create a URL Obj from this. For example :-
String this.url = "http://myweb.com/public/resource/data/stream";
this.url += "?begin=1430295300000&end=1430297279988&id=140621
&time=FIFTEEN_MINUTE";
this.urlObj = new URL(this.url);
Then you create the connection as in your original example.

openConnection will establish the connection and issue the GET. Subsequent settings of GET parameters on the returned URLConnection object are ignored, since the URL has already been open.
Add the parameters to the URL as query string params just as the link above and open that link
or
try to post the parameters to the server (which is even better)

Related

How to send Bearer token with request

I'm attempting to use the Twitter API to return a List of tweets and following this tutorial to invoke a GET request with parameters: https://www.baeldung.com/java-http-request
Here is my code based off tutorial:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class GetTweets {
public static void main(String argsp[]) throws IOException {
URL url = new URL("https://api.twitter.com/2/users/1315617658461659136/tweets");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
Map<String, String> parameters = new HashMap<>();
parameters.put("Token", "******");
con.setDoOutput(true);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(ParameterStringBuilder.getParamsString(parameters));
out.flush();
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
System.out.println("Response is "+content);
}
}
returns:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 405 for URL: https://api.twitter.com/2/users/1315617658461659136/tweets
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1919)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1515)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:250)
at com.reactive.api.twitter.GetTweets.main(GetTweets.java:27)
If I use Postman and set the Bearer token in the Authorization tab the tweets are returned correctly :
So it seems I'm not passing the Bearer token parameter correctly ?
How to pass the Bearer token with the Get request ?
The bearer goes in the "Authorization" header:
con.setRequestProperty("Authorization", "Bearer " + token);

Fatal Error :1:1: Premature end of file - in Url.openStream() / new new XmlReader in Rome tools

Small snippet of program challenging me from a day,
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import com.rometools.rome.io.FeedException;
public class Test {
public static void main(String[] args) throws IllegalArgumentException, FeedException, IOException {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream openStream = new URL("http://www.livemint.com/rss/money").openStream();
Document doc = db.parse(openStream);
System.out.println(doc.getDocumentURI());
} catch (Exception e) {
e.printStackTrace();
}
}
}
All this experiment started from the rome tools parser code, which was giving me same error in different approaches also.
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(new XmlReader(url));
feed.getEntries();
Exception was getting thrown in creating new xml Reader, so i wrote a test program in different approach. Now both are throwing the same error,
[Fatal Error] :1:1: Premature end of file.
I have to fetch xml response from a url, but not able to do.
I believe this is because the server is trying to redirect you to use https instead of http, while HttpURLConnection does not support that automatically.
Changing your URL to use https would solve the issue.
Sending a GET request to this URL from Java Code says:
Sending 'GET' request to URL : http://www.livemint.com/rss/money
Response Code : 301
So the XML is not present in the response as the request fails. Please check the API and see if you have to include any extra parameters in the HTTP request.
I used following code to connect to the URL:
public static String getUrlResponse(String url) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to 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 in String
return response.toString();
}

Server returned HTTP response code: 401 for url when using shopify API

My url as given by the shopify is in this format
https://apikey:password#hostname/admin/orders.json
So when trying to get the orders using HttpURLConnection, I am getting 401 unauthorised error. Here is my code
import java.io.*;
import java.net.*;
import java.util.Properties;
/**
* Created by admin on 22/8/15.
*/
public class Hello {
// This method should be removed in production
static void setProxy(){
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost","lotus");
systemProperties.setProperty("http.proxyPort", "8080");
}
public static void main(String [] args)
{
try
{
setProxy();
URL url = new URL("https://apikey:password#go-frugal.myshopify.com/admin/orders.json");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("user-agent","Mozilla/5.0");
connection.setRequestProperty("Content-Type","application/json");
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String urlString = "";
String current;
while((current = in.readLine()) != null)
{
urlString += current;
}
System.out.println(urlString);
}catch(IOException e)
{
e.printStackTrace();
}
}
}
Here is the error
java.io.IOException: Server returned HTTP response code: 401 for URL: https://apikey:password#go-frugal.myshopify.com/admin/orders.json
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at Hello.main(Hello.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 0<br>
getErrorStream returns this
{"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}
try this ... your call sequence is wrong. hope this help.
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import org.jboss.util.Base64;
public class test9 {
public static void main(String[] args) {
URL url;
URLConnection urlConn = null;
HttpURLConnection htcon = null;
InputStream is = null;
StringBuffer sb = new StringBuffer();
String authStr = "apikey:password";
String authStringEnc = Base64.encodeBytes(authStr.getBytes());
//String authStringEnc = new String(Base64Encoder.encode(authString.getBytes()));
try {
url = new URL("https:go-frugal.myshopify.com/admin/orders.json");
urlConn = url.openConnection();
urlConn.setRequestProperty("Authorization", "Basic " + authStringEnc);
urlConn.setRequestMethod("GET");
urlConn.setRequestProperty("user-agent","Mozilla/5.0");
urlConn.setRequestProperty("Content-Type","application/json");
htcon = (HttpURLConnection) urlConn;
is = htcon.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1024];
while ((numCharsRead = isr.read(charArray)) > 0) {
sb.append(charArray, 0, numCharsRead);
}
System.out.println("sb: "+sb);
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is an oversight in the way Java handles authorization at the connection level. Took me a good long time to debug; it works in the browser, so why doesn't it work in Java?
Turns out that web browsers will automatically encode the authorization token for any URLs with a user info space. Java doesn't do this by default, so you have to do it yourself:
URI uri = new URL("http://username:password#protected.domain.example/resource").toURI();
String userInfo = uri.getRawUserInfo();
if(userInfo != null && userInfo.length() > 0)
userInfo = Base64.getEncoder().encodeToString(userInfo.getBytes());
HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();
if(userInfo != null && userInfo.length() > 0)
connection.setRequestProperty("Authorization", "Basic " + userInfo);
connection.connect();
A few notes:
You can try using URL#getUserInfo() or URI#getUserInfo(), but there's a small chance that it won't encode passwords with legal special characters correctly.
This should work for all HTTP URLs, even if they don't have a user info segment.

Trying to HTTP POST But Getting MalformedURLException: no protocol: yahoo.com

Overall I'm trying to write a script that captures the servers' response to an HTTP POST using java.
Unfortunately, I'm stuck at encoding the URL portion of it. While I followed several online example on encoding a URL, I still get MalformedURLException...
Any idea what might go wrong in the encoding process?
The error:
$ java client_post
Sending Http POST request
Exception in thread "Main Thread" java.net.MalformedURLException: no
protocol: http%3A%2F%2Fyahoo.com
at java.net.URL.<init>(URL.java:567)
at java.net.URL.<init>(URL.java:465)
at java.net.URL.<init>(URL.java:414)
at client_post.sendPost(client_post.java:30)
at client_post.main(client_post.java:23)
The code:
//package client_post;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class client_post {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
client_post http = new client_post();
System.out.println("\nSending Http POST request");
http.sendPost();
}
// HTTP POST request
private void sendPost() throws Exception {
//String url =<host:port/create/service>
String url = "http://yahoo.com";
String EncoderUrl = URLEncoder.encode(url, "UTF-8");
URL obj = new URL(EncoderUrl);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language","en-US,en;q=0.5");
String urlParameters = "<string base64>";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
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());
System.out.println(response.toString());
}
}
When you are encoding url your url becomes like below
http%3A%2F%2Fyahoo.com
Dont encode untill you have something special in it.
Your programm is also throwing class cast exception
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
Above should be like below
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
Below is working programm.
package com.ds.portlet.library;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class client_post {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
client_post http = new client_post();
System.out.println("\nSending Http POST request");
http.sendPost();
}
// HTTP POST request
private void sendPost() throws Exception {
//String url =<host:port/create/service>
String url = "http://yahoo.com";
// String EncoderUrl = URLEncoder.encode(url, "UTF-8");
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language","en-US,en;q=0.5");
String urlParameters = "<string base64>";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
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());
System.out.println(response.toString());
}
}
It looks like you're trying to encode the entire URL, including the :// and similar characters. The purpose of URL encoding is to hide those characters in a path or query part, and they shouldn't be encoded in the main URL. Use URLEncoder only for parameters or application/x-www-form-urlencoded contents.

Urban Airship Push code

I've followed the UA tutorials and got my APID , and successfully recieved test push message on my android device.
Now the next thing that I like to do is to target my device using JAVA and send push messaged.
From what I've seen so far the best way to achieve this is using their web API.
However When I'm trying to send a post message I always get the following error :
java.io.IOException: Server returned HTTP response code: 401 for URL: https://go.urbanairship.com/api/push/
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at com.Sender.Sender.main(Sender.java:56)
This is the code that I use :
package com.Sender;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class Sender {
/**
* #param args
*/
public static void main(String[] args) {
try {
String responseString = "";
String outputString = "";
String username = "MWMoRVhmRXOG6IrvhMm-BA";
String password = "ebsJS2iXR5aMJcOKe4rCcA";
MyAuthenticator ma= new MyAuthenticator(username, password);
Authenticator.setDefault(ma);
URL url = new URL("https://go.urbanairship.com/api/push/");
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) urlConnection;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String APID = "23eef280-19c8-40fc-b798-788f50e255a2";
String postdata = "{\"android\": {\"alert\": \"Hello from JAVA!\"}, \"apids\": [\""+APID+"\"]}";
byte[] buffer = new byte[postdata.length()];
buffer = postdata.getBytes("UTF8");
bout.write(buffer);
byte[] b = bout.toByteArray();
httpConn.setRequestProperty("Content-Length",
String.valueOf(b.length));
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(b);
out.close();
InputStreamReader isr = new InputStreamReader(
httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
while ((responseString = in.readLine()) != null) {
outputString = outputString + responseString;
}
System.out.println(outputString);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Can you help me please ?
HTTP 401 represent Unauthorized access. In your code even though your created Authenticator, you didn't provide it as part of post request header. Here is tutorial on how to set authenticator for a URLConnection.

Categories

Resources