我正在尝试使用Web服务并在该API上进行POST。我和邮递员一起尝试过,一切正常。但是,一旦我编写了一个 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?