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

发布于 2024-11-08 23:34:59 字数 2206 浏览 0 评论 0原文

哇,我只是找不到任何可用的 httpclient 4.1 示例,无法使用非默认端口发送 post 请求 但是可配置端口,我有这段代码,但它无法正常工作

// 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);

并出现此异常:

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)  

简单的话如何定义端口?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清泪尽 2024-11-15 23:35:00

您正在尝试查找主机名 https://foo.com
这就是问题所在;查找应该针对 foo.com。我不确定你是如何得到有问题的代码的,但是值得花一些时间看看 HttpClient 教程

如果我没记错的话,下面的代码应该足够了:

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

其中 url 将使用 http/https 模式、主机名、端口和资源 URI 的其余部分构建。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文