为什么访问外网就不行呢,抛出异常,说是连接被拒绝,多谢各位

解决方案 »

  1.   

    代码如下// 实例化一个HttpClient
    HttpClient httpClient = new DefaultHttpClient();
    // 设定目标站点
    HttpHost proxy = new HttpHost("mail.cdpcb.com", 8080);
    HttpPost post = new HttpPost("http://www.google.com");
    // 设置代理对象 ip/代理名称,端口

    // 对HttpClient对象设置代理
    httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
    proxy); // HttpGet httpGet = new HttpGet("/");
    // 这里也可以直接使用httpGet的绝对地址,当然如果不是具体地址不要忘记/结尾
    // HttpGet httpGet = new HttpGet("http://www.shanhe114.com/");
    // HttpResponse response = httpClient.execute(httpGet); HttpResponse response = httpClient.execute(post);
    if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
    // 请求成功
    // 取得请求内容
    HttpEntity entity = response.getEntity();
    // 显示内容
    if (entity != null) {
    // 显示结果
    BufferedReader reader = new BufferedReader(
    new InputStreamReader(entity.getContent(), "UTF-8"));
    String line = null;
    StringBuffer strBuf = new StringBuffer((int) entity
    .getContentLength());
    while ((line = reader.readLine()) != null) {
    strBuf.append(line);
    }
    strBuf.trimToSize();
    System.out.println(strBuf.toString());
    }
    if (entity != null) {
    entity.consumeContent();
    }
    }else{
    System.out.println("资源请求失败");
    }
    我用的是代码,可是还是请求失败,代理也设置了