HTTPURLConnection int responseCode = connection.getResponseCode(); returns -1 - java

In the code below, hConn.disconnect(); runs. I thought responseCode gets -1. How can I correct it? Is setRequestMethod("GET") necessary?
public static void downloadFile(String fileURL, String saveDirectory)
throws IOException {
URL u = new URL(fileURL);
HttpURLConnection hConn = (HttpURLConnection) u.openConnection();
int responseCode = hConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
//some code here
//some codes here
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
hConn.disconnect();
}

Related

HttpURLConnection.getReadTimeout() / HttpURLConnection.getConnectionTimeout() of -1

I'm trying to determine what the current readTimeout and connectionTimeout is on my Java HttpURLConnection object. They are both returning a -1 with during the logging statements below:
private InputStream getSomethingImportant(final String letterId, final String documentId,
HttpURLConnection connection) throws IOException {
InputStream pdfStream = null;
final String url = this.getBaseURL() + "/letters/" + letterId + "/documents/" + documentId;
connection = RequestResponseUtil.initializeRequest(url, "GET", this.getAuthenticationHeader(), true, MediaType.APPLICATION_PDF_VALUE);
LOG.info("ConnectionTimeout is: {}", connection.getConnectTimeout());
LOG.info("ReadTimeout is: {}", connection.getReadTimeout());
// ...other non-relevant code...
}
public static HttpURLConnection initializeRequest(final String url, final String method,
final String httpAuthHeader, final boolean multiPartFormData, final String responseType) {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod(method);
conn.setRequestProperty("X-Something-Authentication", httpAuthHeader);
conn.setRequestProperty("Accept", responseType);
if (multiPartFormData) {
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=BOUNDARY");
conn.setDoOutput(true);
}
else {
conn.setRequestProperty("Content-Type", "application/xml");
}
}
catch (final MalformedURLException e) {
throw new CustomException(e);
}
catch (final IOException e) {
throw new CustomException(e);
}
return conn;
}
The JavaDocs on getConnectTimeout and getReadTimeout both list 0 as a return option but say nothing about -1. How should I interpret this?
Also, the url I am using is valid and I am returning an InputStream successfully.
Finally, I am using Oracle JDK 1.8.0_77. And, of note, when I actually print out the conn class that is being used at runtime it is weblogic.net.http.SOAPHttpsURLConnection (I am using WebLogic 12.2).
Thank you.

Not getting correct HTTP Response Code in Selenium WebDriver with java

I am trying to do automation of find all the broken link of a page. I have gone through so many articles here but none helped. The real problem I am facing is I am not able to get(returing) the correct httpresponse code. Below is the code:
public static int getResponseCode(String urlString) {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
return connection.getResponseCode();
} catch (Exception e) {
}
return -1;
}
you cant get the response using this code alone using java.
you need the java selenium driver code for this to be implemented.
use the below code to get the right response:
private static int statusCode;
public static void main(String... args) throws IOException{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> links = driver.findElements(By.tagName("a"));
for(int i = 0; i < links.size(); i++){
if(!(links.get(i).getAttribute("href") == null) && !(links.get(i).getAttribute("href").equals(""))){
if(links.get(i).getAttribute("href").contains("http")){
statusCode= intgetResponseCode(links.get(i).getAttribute("href").trim());
if(statusCode == 403){
System.out.println("HTTP 403 Forbidden # " + i + " " + links.get(i).getAttribute("href"));
}
}
}
}
}
public static int getResponseCode(String urlString) throws MalformedURLException, IOException{
URL url = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.setRequestMethod("GET");
huc.connect();
return huc.getResponseCode();
}
Else you can do get the response by setting the response method as "HEAD" [if its a simple test].
hope it helps. Cheers!
The following code worked for me with given example URL:
public static int getResponseCode(String urlString) throws IOException {
// String url = "http://www.google.com/search?q=mkyong";
String url = "https://www.google.co.in/intl/en/about.html?fg=1";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.connect();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
}
Output I got:
Sending 'GET' request to URL : https://www.google.co.in/intl/en/about.html?fg=1
Response Code : 200

HttpURLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL

I am trying to connect to a URL from a desktop app, and I get the error indicated in the Title of my question
Url:https://capi-eval.signnow.com/api/user
The Code fragment.
public void getUrl(String urlString) throws Exception
{
String postData=String.format("{{\"first_name\":\"%s\",\"last_name\":\"%s\",\"email\":\"%s\",\"password\":\"%s\"}}", "FIRST", "LAST","test#test.com","USER_1_PASSWORD");
URL url = new URL (urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Length",""+postData.length());
connection.setRequestProperty ("Authorization", "Basic MGZjY2RiYzczNTgxY2EwZjliZjhjMzc5ZTZhOTY4MTM6MzcxOWExMjRiY2ZjMDNjNTM0ZDRmNWMwNWI1YTE5NmI=");
connection.setRequestMethod("POST");
connection.connect();
byte[] outputBytes =postData.getBytes("UTF-8");
OutputStream os = connection.getOutputStream();
os.write(outputBytes);
os.close();
InputStream is;
if (connection.getResponseCode() >= 400) {
is = connection.getErrorStream();
} else {
is = connection.getInputStream();
}
BufferedReader in =
new BufferedReader (new InputStreamReader (is));
String line;
while ((line = in.readLine()) != null) {
System.out.println (line);
}
}
public static void main(String[] arg) throws Exception
{
System.setProperty("jsse.enableSNIExtension", "false");
Test s = new Test();
s.getUrl("https://capi-eval.signnow.com/api/user");
}
When i dig in the response and print error Stream My output is ::
{"errors":[{"code":65536,"message":"Invalid payload"}]}
any help appreciated
thanks
Maybe something wrong caused by unecoding. Just have a try by using Base64.encode.

java urlconnection get the final redirected URL

I have a url which redirects to another url.I want to be able to get the final redirected URL.My code:
public class testURLConnection
{
public static void main(String[] args) throws MalformedURLException, IOException {
HttpURLConnection con =(HttpURLConnection) new URL( "http://tinyurl.com/KindleWireless" ).openConnection();
System.out.println( "orignal url: " + con.getURL() );
con.connect();
System.out.println( "connected url: " + con.getURL() );
InputStream is = con.getInputStream();
System.out.println( "redirected url: " + con.getURL() );
is.close();
}
}
It always gives original url whereas the redirectURL is:http://www.amazon.com/Kindle-Wireless-Reading-Display-Globally/dp/B003FSUDM4/ref=amb_link_353259562_2?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=center-10&pf_rd_r=11EYKTN682A79T370AM3&pf_rd_t=201&pf_rd_p=1270985982&pf_rd_i=B002Y27P3M.
How can i get this final redirected URL.
Here is what i tried with looping till we get redirects.Still doesent fetch the desired url:
public static String fetchRedirectURL(String url) throws IOException
{
HttpURLConnection con =(HttpURLConnection) new URL( url ).openConnection();
//System.out.println( "orignal url: " + con.getURL() );
con.setInstanceFollowRedirects(false);
con.connect();
InputStream is = con.getInputStream();
if(con.getResponseCode()==301)
return con.getHeaderField("Location");
else return null;
}
public static void main(String[] args) throws MalformedURLException, IOException {
String url="http://tinyurl.com/KindleWireless";
String fetchedUrl=fetchRedirectURL(url);
System.out.println("FetchedURL is:"+fetchedUrl);
while(fetchedUrl!=null)
{ url=fetchedUrl;
System.out.println("The url is:"+url);
fetchedUrl=fetchRedirectURL(url);
}
System.out.println(url);
}
Try this, I using recursively to using for many redirection URL.
public static String getFinalURL(String url) throws IOException {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setInstanceFollowRedirects(false);
con.connect();
con.getInputStream();
if (con.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM || con.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP) {
String redirectUrl = con.getHeaderField("Location");
return getFinalURL(redirectUrl);
}
return url;
}
and using:
public static void main(String[] args) throws MalformedURLException, IOException {
String fetchedUrl = getFinalURL("<your_url_here>");
System.out.println("FetchedURL is:" + fetchedUrl);
}
public static String getFinalRedirectedUrl(String url) {
HttpURLConnection connection;
String finalUrl = url;
try {
do {
connection = (HttpURLConnection) new URL(finalUrl)
.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setUseCaches(false);
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode >= 300 && responseCode < 400) {
String redirectedUrl = connection.getHeaderField("Location");
if (null == redirectedUrl)
break;
finalUrl = redirectedUrl;
System.out.println("redirected url: " + finalUrl);
} else
break;
} while (connection.getResponseCode() != HttpURLConnection.HTTP_OK);
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return finalUrl;
}
My first idea would be setting instanceFollowRedirects to false, or using URLConnection instead.
In both cases, the redirect won't be executed, so you will receive a reply to your original request. Get the HTTP Status value and, if it is 3xx, get the new redirect value.
Of course there may be a chain of redirects, so probably you will want to iterate until you reach the real (status 2xx) page.
#user719950 On my MAC-OSX - this solves the issue of truncated HTTP URL :
To your original code , just add this below line : // You have to find through your browser what is the Request Header IE / Chrome is sending. I still dont have the explanation as why this simple setting is causing correct URL :)
HttpURLConnection con =(HttpURLConnection) new URL
( "http://tinyurl.com/KindleWireless" ).openConnection();
con.setInstanceFollowRedirects(true);
con.setDoOutput(true);
System.out.println( "orignal url: " + con.getURL() );
**con.setRequestProperty("User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2)
AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2
Safari/536.26.17");**
con.connect();
System.out.println( "connected url: " + con.getURL() );
Thread.currentThread().sleep(2000l);
InputStream is = con.getInputStream();
System.out.println( "redirected url: " + con.getURL() );
is.close();
This might help
public static void main(String[] args) throws MalformedURLException,
IOException {
HttpURLConnection con = (HttpURLConnection) new URL(
"http://tinyurl.com/KindleWireless").openConnection(proxy);
System.out.println("orignal url: " + con.getURL());
con.connect();
con.setInstanceFollowRedirects(false);
int responseCode = con.getResponseCode();
if ((responseCode / 100) == 3) {
String newLocationHeader = con.getHeaderField("Location");
responseCode = con.getResponseCode();
System.out.println("Redirected Location " + newLocationHeader);
System.out.println(responseCode);
}
}
#JEETS
Your fetchRedirectURL function may not work because there are a variety of HTTP codes for redirects. Change it to a range check and it will work.
public static String fetchRedirectURL(String url) throws IOException
{
HttpURLConnection con =(HttpURLConnection) new URL( url ).openConnection();
//System.out.println( "orignal url: " + con.getURL() );
con.setInstanceFollowRedirects(false);
con.connect();
InputStream is = con.getInputStream();
if(con.getResponseCode()>=300 && con.getResponseCode() <400)
return con.getHeaderField("Location");
else return null;
}
This one goes recursively in case there are multiple redirects:
protected String getDirectUrl(String link) {
String resultUrl = link;
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) new URL(link).openConnection();
connection.setInstanceFollowRedirects(false);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_MOVED_PERM || responseCode == HttpURLConnection.HTTP_MOVED_TEMP) {
String locationUrl = connection.getHeaderField("Location");
if (locationUrl != null && locationUrl.trim().length() > 0) {
IOUtils.close(connection);
resultUrl = getDirectUrl(locationUrl);
}
}
} catch (Exception e) {
log("error getDirectUrl", e);
} finally {
IOUtils.close(connection);
}
return resultUrl;
}

How to get HTTP response code for a URL in Java?

Please tell me the steps or code to get the response code of a particular URL.
HttpURLConnection:
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
This is by no means a robust example; you'll need to handle IOExceptions and whatnot. But it should get you started.
If you need something with more capability, check out HttpClient.
URL url = new URL("http://www.google.com/humans.txt");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
You could try the following:
class ResponseCodeCheck
{
public static void main (String args[]) throws Exception
{
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
System.out.println("Response code of the object is "+code);
if (code==200)
{
System.out.println("OK");
}
}
}
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;
public class API{
public static void main(String args[]) throws IOException
{
URL url = new URL("http://www.google.com");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
System.out.println(statusCode);
}
}
This has worked for me :
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public static void main(String[] args) throws Exception {
HttpClient client = new DefaultHttpClient();
//args[0] ="http://hostname:port/xyz/zbc";
HttpGet request1 = new HttpGet(args[0]);
HttpResponse response1 = client.execute(request1);
int code = response1.getStatusLine().getStatusCode();
try(BufferedReader br = new BufferedReader(new InputStreamReader((response1.getEntity().getContent())));){
// Read in all of the post results into a String.
String output = "";
Boolean keepGoing = true;
while (keepGoing) {
String currentLine = br.readLine();
if (currentLine == null) {
keepGoing = false;
} else {
output += currentLine;
}
}
System.out.println("Response-->"+output);
}
catch(Exception e){
System.out.println("Exception"+e);
}
}
This is what worked for me:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlHelpers {
public static int getHTTPResponseStatusCode(String u) throws IOException {
URL url = new URL(u);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
return http.getResponseCode();
}
}
Hope this helps someone :)
Efficient way to get data(With uneven payload) by scanner.
public static String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A"); // Put entire content to next token string, Converts utf8 to 16, Handles buffering for different width packets
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
} finally {
urlConnection.disconnect();
}
}
Try this piece of code which is checking the 400 error messages
huc = (HttpURLConnection)(new URL(url).openConnection());
huc.setRequestMethod("HEAD");
huc.connect();
respCode = huc.getResponseCode();
if(respCode >= 400) {
System.out.println(url+" is a broken link");
} else {
System.out.println(url+" is a valid link");
}
This is the full static method, which you can adapt to set waiting time and error code when IOException happens:
public static int getResponseCode(String address) {
return getResponseCode(address, 404);
}
public static int getResponseCode(String address, int defaultValue) {
try {
//Logger.getLogger(WebOperations.class.getName()).info("Fetching response code at " + address);
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(1000 * 5); //wait 5 seconds the most
connection.setReadTimeout(1000 * 5);
connection.setRequestProperty("User-Agent", "Your Robot Name");
int responseCode = connection.getResponseCode();
connection.disconnect();
return responseCode;
} catch (IOException ex) {
Logger.getLogger(WebOperations.class.getName()).log(Level.INFO, "Exception at {0} {1}", new Object[]{address, ex.toString()});
return defaultValue;
}
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
.
.
.
.
.
.
.
System.out.println("Value" + connection.getResponseCode());
System.out.println(connection.getResponseMessage());
System.out.println("content"+connection.getContent());
you can use java http/https url connection to get the response code from the website and other information as well here is a sample code.
try {
url = new URL("https://www.google.com"); // create url object for the given string
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(https_url.startsWith("https")){
connection = (HttpsURLConnection) url.openConnection();
}
((HttpURLConnection) connection).setRequestMethod("HEAD");
connection.setConnectTimeout(50000); //set the timeout
connection.connect(); //connect
String responseMessage = connection.getResponseMessage(); //here you get the response message
responseCode = connection.getResponseCode(); //this is http response code
System.out.println(obj.getUrl()+" is up. Response Code : " + responseMessage);
connection.disconnect();`
}catch(Exception e){
e.printStackTrace();
}
Its a old question, but lets to show in the REST way (JAX-RS):
import java.util.Arrays;
import javax.ws.rs.*
(...)
Response response = client
.target( url )
.request()
.get();
// Looking if response is "200", "201" or "202", for example:
if( Arrays.asList( Status.OK, Status.CREATED, Status.ACCEPTED ).contains( response.getStatusInfo() ) ) {
// lets something...
}
(...)

Categories

Resources