6

I am posting a url with params containg an underscore (_).

sample: http://sdsdsds_asasasahjhd.com/dsdsdsd/login.json?

I am posting it like this:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://sdsdsds_asasasahjhd.com/dsdsdsd/login.json?");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("key1", "value1"));
        nameValuePairs.add(new BasicNameValuePair("key2", "value2"));
        nameValuePairs
                .add(new BasicNameValuePair("key3", "value3"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (Exception e) {
        e.printStackTrace();
    }

When I am inspecting httpclient.execute(httppost) I am getting IllegalArgumentException and in catch in exception details it is telling Host name cannot be null. Please specify any solution.

I have gone through some other questions here:

but no use as I am not encoding the whole url.

Community
  • 1
  • 1
zaiff
  • 999
  • 2
  • 13
  • 29

4 Answers4

1

I have an open-source library with network implementation mechanism. It has just receiver an workaround implementation. All you need it to set the host by reflection in case of troubles:

        final URI uriObj = new URI("https", host, path, null, null);
        if (uriObj.getHost() == null) {
            final Field hostField = URI.class.getDeclaredField("host");
            hostField.setAccessible(true);
            hostField.set(uriObj, host);
        }
        return uriObj;

The commit is here.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0

well it clearly states that the host name can not be null.. your url doesn't specify one..

a url is expected to be in the format

http://hostName.com/example..../example.json
Vinay W
  • 9,912
  • 8
  • 41
  • 47
0

HttpPost httppost = new HttpPost(String);

This contructor will attempt to form a URL instance from the provided String. If this fails, you'll be dealing with a null value.

Please post the actual URL you're using and not a sample if you want us to be able to help you further.

Rawkode
  • 21,990
  • 5
  • 38
  • 45
  • sorry unable to post the actual url.Do you think it is all because of "_"? – zaiff Nov 30 '12 at 13:01
  • no the underscore is not the issue its the format of your URL it does not look like a URL it has to have a tld in it somewhere. – Tkingovr Nov 30 '12 at 13:03
0

See this link. Apache doesnt support underscore. You should change the url

https://issues.apache.org/jira/browse/HTTPCLIENT-911

benoffi7
  • 3,068
  • 3
  • 29
  • 44