developer
developer

Reputation: 9478

i have a very strange issue, url getting appended by / in query string

Below is the url using in my code

URL url = new URL("https://8.7.177.4/ns-api?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001");

but iam getting exception as

java.io.FileNotFoundException: https://8.7.177.4/ns-api/?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at AuthenticateCurl.authenticatenewPostUrl(AuthenticateCurl.java:311)
    at AuthenticateCurl.main(AuthenticateCurl.java:341)

in the exception we can find url where /?object=answerrule is getting appended before starting query string.

How can i resolve this.

Upvotes: 0

Views: 500

Answers (1)

mrb
mrb

Reputation: 3330

When you access the url without the extra '/', the web server forwards you to the version that has the extra '/'. You can see this when attempting to curl the URL at the command line:

$ curl --insecure -v 'https://8.7.177.4/ns-api?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001'
> GET /ns-api?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: 8.7.177.4
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Date: Thu, 12 Jul 2012 18:45:28 GMT
< Server: Apache/2.2.11 (Fedora)
< Location: https://8.7.177.4/ns-api/?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001
< Content-Length: 392
< Connection: close
< Content-Type: text/html; charset=iso-8859-1
<

This is fine, and HttpURLConnection automatically follows the redirect to the new URL. This is normal behaviour.

Following the new URL, we get a different result:

$ curl --insecure -v 'https://8.7.177.4/ns-api/?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001'
> GET /ns-api/?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: 8.7.177.4
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< Date: Thu, 12 Jul 2012 18:46:46 GMT
< Server: Apache/2.2.11 (Fedora)
< X-Powered-By: PHP/5.2.9
< Content-Length: 0
< Connection: close
< Content-Type: text/html; charset=UTF-8
< 

...And we get a 404, which is why you get a FileNotFoundException!

If you were not expecting a redirect and you are running the server as well, maybe there is a configuration problem on the server.

Upvotes: 2

Related Questions