I am trying to parse a https url link and getting a connection reset problem in eclipse , however it works in eclipse ( returns 301 response code though )
How to solve this ?
Thanks
try {
String url = "http://api.demo.globalgatewaye4.firstdata.com/transaction/search?start_date=2018-01-07&end_date=2018-01-09";
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Authorization", "Basic *****************");
System.out.println("Request URL ... " + url);
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
System.out.println("Response Code ... " + status);
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
System.out.println("Redirect to URL : " + newUrl);
}
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
html.append(inputLine);
}
in.close();
System.out.println("URL Content... \n" + html.toString());
System.out.println("Done");
} catch (Exception e) {
e.printStackTrace();
}
This returns a HTTP response code "301" which redirects it to a HTTPS url which in turn give s a connection reset error .
How to solve this error in eclipse , or is there a generic way .
Thank you . Found the solution . problem was related to TLS not being available in the java version i was using .
Had to switch to a higher java version .
Related
I am working on application to access a web page through vpnbook.com.
My code is sending post request to URL "https://webproxy.vpnbook.com/includes/process.php?action=update" with post body as "u=yahoo.com&webproxylocation=random"
i am getting response code 302. and HTTP response header contains location
"Location=[http://www.vpnbook.com/webproxy]"
But same request was getting correct correct result when opening through web browser.
result contains header response
"Location:https://usproxy.vpnbook.com/browse.phpu=DWnjEwVwhlG5GQhW&b=0&f=norefer"
please tell me what is going wrong in my code.
try{
URL url = new URL("https://webproxy.vpnbook.com/includes/process.php?action=update");
HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
c.setRequestProperty("User-Agent", USER_AGENT);
c.setRequestProperty("Accept-Language", ACCEPT_LANG);
c.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
c.setRequestProperty("Host", "webproxy.vpnbook.com");
c.setRequestProperty("Origin", "http://www.vpnbook.com");
c.setRequestProperty("Referer", "http://www.vpnbook.com/webproxy");
c.setRequestMethod("POST");
c.setDoInput(true);
c.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(c.getOutputStream());
wr.writeBytes("u=yahoo.com&webproxylocation=random");
wr.flush();
wr.close();
int responseCode = c.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
System.out.println("Response headers : " + c.getHeaderFields());
Map<String, List<String>> resHdr = c.getHeaderFields();
if( resHdr.containsKey("Set-Cookie") ){
cookies=resHdr.get("Set-Cookie").toString();
cookies = cookies.replaceAll("\\[|\\]", "");
System.out.println(cookies);
}
BufferedReader in =
new BufferedReader(new InputStreamReader(c.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response);
}catch(Exception ex) { ex.printStackTrace(); }
I am trying to send notification message for IOS through GCM from JAVA. That time i am getting one error with 400 status code Bad Request. Below i have mentioned my JAVA code...
String apiKey = "xxxxxxxxxxxxxxxxxxxx"; // Put here your API key
String GCM_Token = regid; // put the GCM Token you want to send to here
String notification = "{\"sound\":\"default\",\"badge\":\"2\",\"title\":\"default\",\"body\":\"Test Push!\"}"; // put the message you want to send here
String messageToSend = "{\"to\":\"" + GCM_Token + "\",\"notification\":" + notification + ",\"content_available\" : true}"; // Construct the message.
TRY{
URL url = new URL("https://android.googleapis.com/gcm/send");
System.out.println("Message"+messageToSend);
// Open connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Set the headers
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setRequestProperty("Authorization", "key=" + apiKey);
conn.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.write(messageToSend.getBytes("UTF-8"));
//Send the request and close
wr.flush();
wr.close();
//Get the response
int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
System.out.println("Message : " + conn.getResponseMessage());
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//Print result
System.out.println(response.toString()); //this is a good place to check for errors using the codes in http://androidcommunitydocs.com/reference/com/google/android/gcm/server/Constants.html
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
please give me the suggestion.Thanks in advance.
I have a cron job schedule in my Ubuntu or Runnable JAR running.
I have received an exception on my string URL but on my Windows development it is OK. it could not resolve the correct string url please check below
java.io.FileNotFoundException: http://www.carlolotti.com/enoteca-vini-valdostani/chardonnay-elevé-en-fut-de-chene-anselmet-2007-6s0jwecq.asp
here is the source code:
HttpURLConnection conn;
URL obj = new URL(url);
conn = (HttpURLConnection) obj.openConnection();
// default is GET
conn.setRequestMethod("GET");
conn.setUseCaches(true);
// act like a browser
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
int responseCode = conn.getResponseCode();
if(debug)
System.out.println("\nSending 'GET' request to URL : " + url);
if(debug)
System.out.println("Response Code : " + responseCode);
try{
BufferedReader in =
new BufferedReader(new InputStreamReader(conn.getInputStream() , "UTF-8"));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
//inputLine=StringEscapeUtils.escapeHtml3(inputLine);
//if(inputLine.contains("Albari"))
// t=1;
response.append(inputLine);
if(csv)
response.append(lineRet);
}
in.close();
return response;
}catch(Exception e){
e.printStackTrace();
}
return null;
I'm suspecting the Language of my Ubuntu on my Locale LANG=en_GB.UTF-8 Do I need to change this to en_US.UTF-8? I'm not really sure about it but this is my 1st investigation.
You have to replace the éin the url with é.
So I want to send a GET request with parameters. But it only seems to have conventions for the url you send the request to. Unlike the POST request, I see no way to pass parameters in it.
How I send the GET request now, without parameters (might be wrong):
String url = "http://api.netatmo.net/api/getuser";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
Log.v(TAG, ("\nSending 'GET' request to URL : " + url));
Log.v(TAG, ("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
Log.v(TAG, (response.toString()));
How I send the POST request with parameters:
String url = "https://api.netatmo.net/oauth2/token";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add request header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "grant_type=password&client_id=myid&client_secret=mysecret&username=myusername&password=mypass";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
Log.v(TAG, "\nSending 'POST' request to URL : " + url);
Log.v(TAG, "Post parameters : " + urlParameters);
Log.v(TAG, "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
Log.v(TAG, response.toString());
access_token = response.substring(17, 74);
refresh_token = response.substring(93,150);
getRequest = "/api/getuser?access_token=" + access_token + " HTTP/1.1";
Log.v(TAG, access_token);
Log.v(TAG, refresh_token);
Log.v(TAG, getRequest);
As per the HTTP specification GET supports only path params or url params and hence you cannot put the params in HTTP request body as you do in POST request.
As Sotirios mentioned in the comments, technically you can still push params in the GET body, but if the APIs are respecting the specs, they will not provide you a way to do it.
Have you tried to add the query params to the request java.net.URL?
String url = "http://api.netatmo.net/api/getuser?access_token=" + access_token;
URL obj = new URL(url);
I was encountering the same problem, trying this:
String bla = "http://api.netatmo.net/api/devicelist?access_token=" + AUTH_TOKEN;
URL url = new URL(bla);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line = "";
String message = "";
while ((line = reader.readLine()) != null)
{
message += line;
}
I got an exception that the syntax was not correct. When I changed the syntax (by for example encoding with UTF 8) the API would just return errors (like 404 not found...).
I finally got it working using this:
try
{
System.out.println("Access Token: " + AUTH_TOKEN);
String url = "http://api.netatmo.net/api/devicelist";
String query = "access_token=" + URLEncoder.encode(AUTH_TOKEN, CHARSET);
URLConnection connection = new URL(url + "?" + query).openConnection();
connection.setRequestProperty("Accept-Charset", CHARSET);
InputStream response = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
String line = "";
String message = "";
while ((line = reader.readLine()) != null)
{
message += line;
}
return message;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Note: CHARSET = "UTF-8"
Turns out the url the API provided confused me greatly. I fixed the url and it works now.
I am using simple urlconnection like this:
url = URL+"getClient&uid="+cl_id;
URL url = new URL(this.url);
Log.d("Set++","get_t URL: "+ url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
its working fine, but sometimes i get this error:
error: java.net.SocketTimeoutException: Connection timed out
what could be the reason? I have only 4 clients... so i dont think that the server is overloaded with connections.
the code:
try {
URL = Settings.BASE_URL + "_interface.php?" +
"key=" + Settings.KEY +"&app_naam="+Settings.APP_NAAM+ "&action=check&setTime="+c;
URL url = new URL(URL);
if(D)Log.e("ChekTreadAanvr+url", URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String response;
if ((response = rd.readLine()) != null) {
rd.close();
}
if(D) Log.d("WebSaveThread+","DATARESIEVED: "+response);
return response;
} catch (MalformedURLException e) {
if(D)Log.d("ERR","server chekc failure ++ ");
return success = false;
} catch (IOException ioex) {
if(D)Log.e("ERR", "error: " + ioex.getMessage(), ioex);
success = false;
}
return Boolean.toString(success);
It may be possible that your Internet Connection Speed is weak. You can increase your Timeout Interval by using following method.
httpURLConnection.setConnectTimeout( 6 * 10 * 1000 ); // One Minute
If you are connecting to a particularly slow server, there are two timeouts that you can set: setConnectTimeout() and setReadTimeout(). The names are fairly self explanatory. For example:
httpURLConnection.setConnectTimeout(60*1000);
httpURLConnection.setReadTimeout(60*1000);