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

8/22/2022 - Mon
bluebelldiscovery

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

CEHJ

>>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

Mick Barry

> 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.

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
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
Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
galaxy573

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.