Avatar of galaxy573
galaxy573

asked on 

HttpURLConnection not returning error

I'm using HttpURLConnection to send a request to a server.  

When I send invalid information (such as "test") I should get a Error 404 or Bad Gateway however the ResponseCode being returned is 200 & the ResponseMessage is "OK" as if it went through ok.

What do I do to catch the error code?

public InputStream submitRequest(String strRqstURL) {
 
rqstURL = new URL(strRqstURL);
HttpURLConnection con = (HttpURLConnection)rqstURL.openConnection();
int resp = con.getResponseCode();
String respMsg = con.getResponseMessage();
 
System.out.println("response code: " + resp);
System.out.println("response message: " + respMsg);
}

Open in new window

JavaFile Sharing Software

Avatar of undefined
Last Comment
galaxy573
Avatar of bluebelldiscovery
bluebelldiscovery
Flag of Singapore image

Try this...
	static public InputStream submitRequest(String strRqstURL) throws Exception
	{
		URL rqstURL = new URL(strRqstURL);
		HttpURLConnection con = (HttpURLConnection)rqstURL.openConnection();
		int resp = con.getResponseCode();
		String respMsg = con.getResponseMessage();
		 
		System.out.println("response code: " + resp);
		System.out.println("response message: " + respMsg);
 
		if(resp==404)
			throw new Exception("Check Exception here:"+respMsg);
 
		return con.getInputStream();
	}
 
	public static void main(String[] arguments) throws Exception
	{
		submitRequest("http://test");
	}

Open in new window

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>When I send invalid information

How do you send it? The page could well be simply ignoring your 'parameter', even redirecting you to another page entirely. You might try the following at the top of your code to see what happens:
HttpURLConnection.setFollowRedirects(false);

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

> When I send invalid information (such as "test") I should get a Error 404 or Bad Gateway however the ResponseCode being returned is 200 & the ResponseMessage is "OK" as if it went through ok.


Why do u expect a 404? Just because you think its invalid doesn't necessarily mean it will return a 404, it depends on how the server is configured.

You need to check exactly what the server is expected to return.

Try loading the url with a browser and see what it returns.

Avatar of PHPaul
PHPaul

Is the page you are accessing generated by you? If so, are you setting the right headers to indicate a 404?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of galaxy573
galaxy573

ASKER

You are correct.  What was happening is the request was being sent & the server was responding with no problem (hence the 200 status).  I had a problem with Exception catching.  I was not parsing the response correctly and throwing an Exception when the server replied with Error in the xml response.
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo