• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Httpclient - host parameter is null - invalid port number

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


for the above code I am getting java.lang.IllegalArgumentException: host parameter is null Exception.
Then I googled the issue and came up with a solution from here :

But now the exception is changed saying : org.apache.commons.httpclient.URIException: invalid port number
The port number I used 443 because it is https connection.
Now I have no clue what to do and don't even found any solution on google.
Can someone please have some idea to solve this. Any help is heartily appreciated.

Thanks
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:
The port number I used 443 because it is https connection.



If you want the https protocol, you should specify it. From your code, I think that you are using the default protocol, which is http.

Henry
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:If you want the https protocol, you should specify it. From your code, I think that you are using the default protocol, which is http.
Henry


Hello Henry,
But that is not the case I guess. I am giving you the proof. Due to some confidential data issue I cannot share the exact endpointUrl but I am giving you a dummy endpointUrl.
I have a endpointUrl https://abcxyz.com/incident.do?CSV (abcxyz is the only dummy part here), Now this url is returning me the CSV file what I requested by using the below code :


But when I changes the endpointUrl to https://abcxyz.com/incident.do?CSV&sys_param_query=active=true^sys_updated_onBETWEENjavascript:gs.dateGenerate(%272016-11-20%27,%2700:10:00%27)@javascript:gs.dateGenerate(%272016-11-24%27,%2712:59:59%27) in the same above code, it complains me : java.lang.IllegalArgumentException: Invalid uri . Then I copy pasted this url and noticed that web browser is downloading a CSV for me. So, I googled and came up with solution to encode the url. So my code became :


Now the Invalid uri problem got solved and a new exception came up : java.lang.IllegalArgumentException: host parameter is null, for which again I googled the query and came up with setting up the host configuration :

Again, the problem get resolved and a new problem came up : org.apache.commons.httpclient.URIException: invalid port number

So this was the whole flow of solutions-errors what I faced.  But now I am stucked, I have no clue what to do next and my project is on hold due to this. Please help.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The colon characters in the URL might be causing the problem.  You need to encode your URL to ensure that it does not contain any reserved characters.

According to rfc3986 the reserved characters are:
    :/?#[]@!$&'()*+,;=

Edit: Sorry - I just noticed that you are alrady encoding the URL
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you are working with an old version of the Apache libraries.  Here is an bare-bones example using 4.5.2 (available here):

Console:
Result code: 200
Returned content: {
 "authenticated": true,
 "user": "user"
}

 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ron.
The solution you provided is working fine for :

But giving
Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in query at index 76: https://abcxyz.com/incident.do?CSV&sys_param_query=active=true^sys_updated_onBETWEENjavascript:gs.dateGenerate(%272016-11-20%27,%2700:10:00%27)@javascript:gs.dateGenerate(%272016-11-24%27,%2712:59:59%27)
at java.net.URI.create(URI.java:852)
at Testclass.main(Testclass.java:33)
Caused by: java.net.URISyntaxException: Illegal character in query at index 76: https://abcxyz.com/incident.do?CSV&sys_param_query=active=true^sys_updated_onBETWEENjavascript:gs.dateGenerate(%272016-11-20%27,%2700:10:00%27)@javascript:gs.dateGenerate(%272016-11-24%27,%2712:59:59%27)
at java.net.URI$Parser.fail(URI.java:2848)
at java.net.URI$Parser.checkChars(URI.java:3021)
at java.net.URI$Parser.parseHierarchical(URI.java:3111)
at java.net.URI$Parser.parse(URI.java:3053)
at java.net.URI.<init>(URI.java:588)
at java.net.URI.create(URI.java:850)
... 1 more
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:Exception in thread "main" java.lang.IllegalArgumentException: Illegal character in query at index 76


The illegal character is the caret (^) -- for a quick fix, replace it with %5E.

For a longer-term fix, re-think what is going in to the URL.  Putting what appears to be executable code in the query string seems like bad idea.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the solution.
Firstly I treid to encode the URI

but that gave me org.apache.http.ProtocolException: Target host is not specified

So, what I found is that do not encode the whole URI, instead encode it partially. like this:

Now the program is working fine but I think this is the last issue I am facing now.
after the uri is encoded, that becomes :
https://abcxyz.com/incident.do?CSV%26sys_param_query%3Dactive%3Dtrue%5Esys_updated_onBETWEENjavascript%3Ags.dateGenerate%282016-11-20%2C00%3A10%3A00%29%40javascript%3Ags.dateGenerate%282016-11-24%2C12%3A59%3A59%29
when I hit the unencoded uri into the web browser, that downloads a csv for me, but when i hit this encoded uri in web browser, that opens a html page for me which of no use. How can I overcome this?
 
Ron McLeod
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe it has something to do with the single-quote character in the dates being escaped twice.

Why is your un-escaped URL using the form %272016-11-24%27,%2712:59:59%27 rather than '2016-11-24','12:59:59' ?
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:Why is your un-escaped URL using the form %272016-11-24%27,%2712:59:59%27 rather than '2016-11-24','12:59:59' ?


I replaced the spaces with %27

Ron McLeod wrote:The illegal character is the caret (^) -- for a quick fix, replace it with %5E.


This worked well for me. Thanks Ron

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic