Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache HttpClient 3,多线程,每个请求具有不同的凭据?_Java_Httpclient - Fatal编程技术网

Java Apache HttpClient 3,多线程,每个请求具有不同的凭据?

Java Apache HttpClient 3,多线程,每个请求具有不同的凭据?,java,httpclient,Java,Httpclient,如何在HttpClient上为每个请求提供特定的凭据? 我现在的问题是第二个线程似乎取代了以前的第一个线程凭证 下面是我的示例代码: class GetThread extends Thread { HttpClient httpClient; private String username,password; public GetThread(HttpClient httpClient,String username,String password) {

如何在HttpClient上为每个请求提供特定的凭据? 我现在的问题是第二个线程似乎取代了以前的第一个线程凭证

下面是我的示例代码:

    class GetThread extends Thread {
    HttpClient httpClient;

    private String username,password;

    public GetThread(HttpClient httpClient,String username,String password) {
        this.httpClient = httpClient;
        this.username= username;
        this.password= password;
    }

    public void run() {

        Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
        httpClient.getState().setCredentials(new AuthScope("dummyhost", 80, AuthScope.ANY_REALM), defaultcreds);
        HttpMethod method = new GetMethod("http://dummyhost/RSL/servlets/dv.data");

        method.setDoAuthentication(true);
        try {
            httpClient.executeMethod(method);

            byte[] responseBody = method.getResponseBody();
            System.out.println(Thread.currentThread().getName()+" "+username+" "+new String(responseBody));

        } catch (Exception e) {
            e.printStackTrace();
        } 
         finally {
             method.releaseConnection();
         }

    }
}
这是我在我的主要课程中所做的:

    MultiThreadedHttpConnectionManager connectionManager = 
        new MultiThreadedHttpConnectionManager();

    HttpClient httpClient = new HttpClient(connectionManager);
    GetThread getThread[] = {new GetThread(httpClient, "rsbatch1", "test1234"),
            new GetThread(httpClient, "rsbatch12", "test1234")};

    for(int i=0;i<getThread.length;i++)
    {
        getThread[i].start();
    }
多线程HttpConnectionManager连接管理器=
新的多线程HttpConnectionManager();
HttpClient HttpClient=新的HttpClient(connectionManager);
GetThread GetThread[]={new GetThread(httpClient,“rsbatch1”,“test1234”),
新的GetThread(httpClient,“rsbatch12”,“test1234”);

对于(int i=0;i如果您希望每个线程有单独的凭据,那么您还需要每个线程有一个单独的客户端-每个客户端有一组凭据

将GetThread的构造函数更改为

public GetThread(HttpConnectionManager connectionManager,String username,String password) {
    this.httpClient = new HttpClient(connectionManager);
    this.username= username;
    this.password= password;
}
这应该满足你的需要