java http 接口 配置,Java httpclient 4.1如何使用可配置端口发送post请求

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 params = new ArrayList();

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 ?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值