日期:2014-05-20  浏览次数:21162 次

httpclient post提交报java.net.UnknownHostException
我做了个简单的httpclient post 提交
不知为何总报java.net.UnknownHostException异常。

Java code

url="http://www.dianpin.com";
        CityDetails citys = new CityDetails();
        // String targetRequest=citys.getCityDetails();
        String targetRequest = "aaa";
        HttpClient httpClient = new HttpClient();
        httpClient.getHostConfiguration().setHost(url);
        // 创建POST方法的实例
        PostMethod postMethod = new PostMethod();
        // 使用系统提供的默认的恢复策略
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                new DefaultHttpMethodRetryHandler());
        postMethod.setRequestHeader( "User-Agent", "fake"); 
        // 填入各个表单域的值
        NameValuePair[] data = { new NameValuePair("request", targetRequest) };
        // 将表单的值放入postMethod中
        postMethod.setRequestBody(data);
        try {
            // 执行postMethod
            int statusCode = httpClient.executeMethod(postMethod);
            // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
            // 301或者302
            if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
                    || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                // 从头中取出转向的地址
                Header locationHeader = postMethod
                        .getResponseHeader("location");
                String location = null;
                if (locationHeader != null) {
                    location = locationHeader.getValue();
                    System.out
                            .println("The page was redirected to:" + location);

                } else {
                    System.err.println("Location field value is null.");
                }

            }
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: "
                        + postMethod.getStatusLine());
            }
            // 读取内容
            byte[] responseBody = postMethod.getResponseBody();
            // 处理内容
            String str = new String(responseBody);
            //writeXML(str);
            System.out.println(str);

        } catch (HttpException e) {
            // 发生致命的异常,可能是协议不对或者返回的内容有问题
            System.out.println("Please check your provided http address!");
            e.printStackTrace();
        } catch (IOException e) {
            // 发生网络异常
            e.printStackTrace();
        } finally {
            // 释放连接
            postMethod.releaseConnection();
        }
    }



url换成我自己localhost:8081/ssh2/hello.action
也不行,但是在ie里面输入这个地址是可以运行的。
请问,这个问题如何解决,是由什么原因造成的

------解决方案--------------------
http://localhost:8081/ssh2/hello.action
------解决方案--------------------
java.net.UnknownHostException
找不到主机,你先检查一下你的ip配置什么的看看
------解决方案--------------------
探讨

java.net.UnknownHostException
找不到主机,你先检查一下你的ip配置什么的看看

------解决方案--------------------
HttpPost post = new HttpPost(URL);