How to Get Http Response Code for a Url in Java

get http response code from a url

I tried the your code as the following and it worked fine for me:

import java.net.*;

public class Action
{

public static void main(String[] args)
{
try
{
URL url = new URL("http://localhost:8888");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();

int code = connection.getResponseCode();
System.out.println("code: "+code);
}
catch(Exception e)
{

}

}
}


also with google.

How to get HTTP Response Code using Selenium WebDriver

In a word, no. It's not possible using the Selenium WebDriver API. This has been discussed ad nauseam in the issue tracker for the project, and the feature will not be added to the API.

Force java.net.HttpUrlConnection to return GET-response regardless of Http-Status-Code

I don't believe you can do that, but there's https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html#getErrorStream-- for getting the payload in case of an error.

Getting java.io.IOException: Server returned HTTP response code: 400 for URL: when using a url which return 400 status code

In the question since we are getting 400 status code for GET request. So in built XmlSlurper().parse(URI) method does not work as it throw io.Exception.
Groovy also support HTTP methods for api request and response and the below worked for me:

def getReponseBody(endpoint) {     
URL url = new URL(endpoint)
HttpURLConnection get = (HttpURLConnection)url.openConnection()
get.setRequestMethod("GET")
def getRC = get.getResponseCode()

BufferedReader br = new BufferedReader(new InputStreamReader(get.getErrorStream()))
StringBuffer xmlObject = new StringBuffer()
def eachLine
while((eachLine = br.readLine()) !=null){
xmlObject.append(eachLine)
}
get.disconnect()
return new XmlSlurper().parseText(xmlObject.toString())
}

How can I get http response body when request is failed in java?

Try the below code :

package com.abc.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String url = "http://localhost:8888/login?token=token";
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
int responseCode = connection.getResponseCode();
InputStream inputStream;
if (200 <= responseCode && responseCode <= 299) {
inputStream = connection.getInputStream();
} else {
inputStream = connection.getErrorStream();
}

BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));

StringBuilder response = new StringBuilder();
String currentLine;

while ((currentLine = in.readLine()) != null)
response.append(currentLine);

System.out.println(response.toString());
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Server returned HTTP response code: 500. Java app

I have added:

urlConnect.setRequestProperty("Accept", "application/json");

then I can successfully get the response.



Related Topics



Leave a reply



Submit