Java 使用web服务时出现未知主机异常
我正在尝试使用一个web服务并在该api上发表文章。我和邮递员试过了,一切都很好。但是,一旦我编写了一个java客户机来使用该web服务,我就会得到以下错误Java 使用web服务时出现未知主机异常,java,web-services,exception,post,rest-client,Java,Web Services,Exception,Post,Rest Client,我正在尝试使用一个web服务并在该api上发表文章。我和邮递员试过了,一切都很好。但是,一旦我编写了一个java客户机来使用该web服务,我就会得到以下错误 java.net.UnknownHostException: www.api.com at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.ja
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();
}
public static void sendPost()抛出ClientProtocolException,IOException{
试一试{
HttpClient=new DefaultHttpClient();
HttpPost-HttpPost=新的HttpPost(_api.trim());
List postParameters=new ArrayList();
后参数。添加(新的BasicNameValuePair(“parm_1”、“dgs”);
后参数。添加(新的BasicNameValuePair(“parm_2”,“ab”);
后参数。添加(新的BasicNameValuePair(“parm_3”、“4hdiin”);
setHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
setEntity(新的UrlEncodedFormEntity(后参数));
HttpResponse response=client.execute(httpPost);
}捕获(例外e){
e、 printStackTrace();
}
来自
抛出UnknownHostException以指示无法确定主机的IP地址
您是否尝试在url www.api.com之前添加http或https