HttpClient连接问题

时间:2013-06-20 14:28:44

标签: java apache http httpclient

我正在尝试使用Java.net Lib通过代码连接网页,但由于公司代理设置,我无法连接。我看到了httpclient示例代码here,但没有运气。

  

执行requestGET http://www.google.com/ HTTP / 1.1   强文本线程中的异常“main”java.net.SocketException:Socket已关闭           at java.net.PlainSocketImpl.socketConnect(Native Method)           在java.net.PlainSocketImpl.doConnect(未知来源)           at java.net.PlainSocketImpl.connectToAddress(Unknown Source)           在java.net.PlainSocketImpl.connect(未知来源)           在java.net.SocksSocketImpl.connect(未知来源)           在java.net.Socket.connect(未知来源)           at org.apache.http.conn.util.SocketUtils.connect(SocketUtils.java:104)           at org.apache.http.conn.util.SocketUtils.connect(SocketUtils.java:70)           在org.apache.http.conn.PlainSocketFactory.connectSocket(PlainSocketFactory.java:109)           at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:134)           在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:149)           在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)           at org.apache.http.impl.client.DefaultClientRequestDirector.execute(DefaultClientRequestDirector.java:302)           在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:500)           在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:455)           在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:421)           在ClientAuthentication.main(ClientAuthentication.java:52)

代码是

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ClientAuthentication {

    public static void main(String[] args) throws Exception {
        final String authUser = "10605868";
        final String authPassword = "Mouse19*5";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getCredentialsProvider().setCredentials(
                    new AuthScope("172.20.10.18", 8002),
                    new UsernamePasswordCredentials(authUser, authPassword));

            HttpGet httpget = new HttpGet("http://www.google.com/");

            System.out.println("executing request  " + httpget.getRequestLine());
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: "
                        + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }
}

**我知道为什么我会遇到这个问题吗?

添加到这篇文章:

  1. 我正在使用Linux(ubuntu)
  2. 代理商明细是正确的

  3. 我尝试过使用System.prop和proxy和Authenticator方法但授权issue **

0 个答案:

没有答案