gpt4 book ai didi

java - 使用 Web 服务时出现 UnKnowHost 异常

转载 作者:行者123 更新时间:2023-12-02 11:21:51 25 4
gpt4 key购买 nike

我正在尝试使用 Web 服务并在该 api 上执行 POST。我已经和 postman 一起尝试过,一切顺利。但是,一旦我编写了一个 java 客户端来使用该 Web 服务,我就会收到以下错误。

java.net.UnknownHostException: www.api.com
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323)
at java.net.InetAddress.getAllByName0(InetAddress.java:1276)
at java.net.InetAddress.getAllByName(InetAddress.java:1192)
at java.net.InetAddress.getAllByName(InetAddress.java:1126)
at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:259)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:304)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)

Java 客户端代码:

public static void sendPost() throws ClientProtocolException, IOException {

try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(_api.trim());
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("parm_1", "dgs"));
postParameters.add(new BasicNameValuePair("parm_2", "ab"));
postParameters.add(new BasicNameValuePair("parm_3", "4hdiin"));
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");

httpPost.setEntity(new UrlEncodedFormEntity(postParameters));

HttpResponse response = client.execute(httpPost);
} catch (Exception e) {
e.printStackTrace();
}

最佳答案

来自javadocs

抛出UnknownHostException表示无法确定主机的IP地址。

您是否尝试过在网址 www.api.com 之前添加 http 或 https?

关于java - 使用 Web 服务时出现 UnKnowHost 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49853126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com