0

ユーザー名とパスワードを使用して Web サービスにログインしようとして認証しています。次に、後続の呼び出しで sessionid を使用する必要があります。Apache HttpClient (レガシーバージョン) 2.0.2 を使用しています。

以下は、認証するクライアント側のコードです。私の質問は、認証後にセッション ID を取得する方法と、その後の呼び出しで同じセッション ID を使用する方法です。

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
public class TestHttpClient {

    public static void main(String args[]) {
        try {
            HttpClient client = new HttpClient();
            GetMethod get = new HttpGet("www.google.com");

            org.apache.commons.httpclient.UsernamePasswordCredentials upc =
                    new org.apache.commons.httpclient.UsernamePasswordCredentials("user", "pwd");

            client.getState().setCredentials(null, null, upc);

            get.setDoAuthentication(true);

            client.setConnectionTimeout(60000);

            client.executeMethod(get);

            System.out.println(get.getResponseBodyAsString());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Web 開発の初心者を助けてくれてありがとう。

4

1 に答える 1

1

セッション ID を取得する方法は次のとおりです。

.......

client.executeMethod(get); 

Header[] headers=get.getResponseHeader("jsessionid");

if(headers!=null && headers.length>0){

 String sessionId=headers[0].getValue();

}

セッションIDを往復させずに、認証されたセッションを処理するより良い方法があると確信しています。

于 2013-02-27T20:27:41.210 に答える