1

wow , i just can't find any working httpclient 4.1 example , to send post request with Not default port but configurable port , i have this code but its dosn't work

// lHashMapParams is with params getting form function .. List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); for (String key : lHashMapParams.keySet()) { String val = lHashMapParams.get(key); params.add(new BasicNameValuePair(key,val)); } String url = "https://foo.com"; int port = 8883; String = "https"; UrlEncodedFormEntity query = new UrlEncodedFormEntity(params); HttpHost httpHost = new HttpHost(url,port,httpType); HttpPost post = new HttpPost("/"); post.setEntity(query); HttpResponse response_ = httpclient.execute(httpHost,post); 

getting this exception :

log4j:WARN Please initialize the log4j system properly. java.net.UnknownHostException: https://foo.com at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:849) at java.net.InetAddress.getAddressFromNameService(InetAddress.java:1183) at java.net.InetAddress.getAllByName0(InetAddress.java:1136) at java.net.InetAddress.getAllByName0(InetAddress.java:1109) at java.net.InetAddress.getAllByName(InetAddress.java:1072) at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:242) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:130) at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:121) at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:561) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:415) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:776) at com.ut.HttpClientImpl.postHttpReqest(HttpClientImpl.java:75) 

is simple words how to define port ?

4
  • 5
    That code says "unknown host" and nothing about the port. The port is the part of the URL. http://foo.com:8888 will try to connect to foo.com on port 8888. Commented May 23, 2011 at 12:17
  • i know its like it dosn't know the port even so i did put value in the new HttpHost(url,port,httpType); Commented May 23, 2011 at 12:21
  • That's not the point, though - the host is not found in DNS, the port is not relevant. Commented May 23, 2011 at 12:34
  • when i excute the code in this form : androidsnippets.com/… every thing is working fine Commented May 23, 2011 at 12:37

1 Answer 1

4

You are attempting to perform a lookup of the host name https://foo.com and that's the problem; the lookup should have been against foo.com. I'm not sure as to how you've arrived at the code in question, but it would be worth some time to take a look at the HttpClient tutorial.

If I'm not wrong, the following code, should be sufficient:

HttpPost httpost = new HttpPost(url); //construct the complete URL i.e. action to post to httpost.setEntity(entity); response = httpclient.execute(httpost); 

where url is to be constructed with the http/https schema, host name, port and the rest of the resource URI.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, take a look at the example for form-based login at the examples page for HttpClient.
i saw all of them this is way i asked the question , way there isn't some method to set port like C cUrl , why i need to build the string its stupid
Maybe you should spend more time on the tutorial. Chapter 1 shows how to accomplish this using URIUtils.
not good i still need to build the query , way not use some kind of vector of per's
If you're using POST, why do you need to build the query string? Why not use the POST Body, which happens to the HTTP Entity that you're apparently referring to? I think you need to understand the purpose of the classes in HttpClient before you proceed with programming against it. Understanding the object model of HttpClient and mapping it to the HTTP protocol goes a long way in ensuring that you will be able to accomplish any aspect of the HTTP protocol that HttpClient supports.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.