http プログラミングは初めてで、API を使用しない Web サイトに対して認証しようとしていますが、問題が発生しています。私と似ていると思われる他のいくつかの質問を見つけましたが、私に合った答えはありませんでした.
私はいくつかの異なるアプローチを試しましたが、まだ機能するものを見つけていません。実際、私は一度それを機能させましたが、そのコードをチェックインしませんでした (私は知っています - 私は何を考えていましたか?)。私は再びその作業バージョンに戻ることができませんでした。これまでに試したいくつかのことを次に示します。
HttpClient client = new DefaultHttpClient(); //or any method to get a client instance, with a 'threadsafe' connection manager or otherwise
Credentials credentials = new UsernamePasswordCredentials(userName, passwd);
((DefaultHttpClient) client).getCredentialsProvider()
.setCredentials(AuthScope.ANY, credentials);
// website is defined as my target website & this constitutes the valid login URL
HttpPost post = new HttpPost(https + website + "/login");
HttpEntity entity = new StringEntity("Username="+ userName +"&Password="+ passwd);
post.setEntity(entity);
HttpResponse response = client.execute(post);
EntityUtils.consume(entity);
entity = response.getEntity();
EntityUtils.consume(entity);
// the protected part of the site is over http after authentication succeeds
HttpGet get = new HttpGet(http + website +"/protected");
response = client.execute(get);
entity = response.getEntity();
String content = EntityUtils.toString(entity);
この時点で、Web サイトから返される「エンティティ」は「エラー」:「不正アクセス」です。
「Credentials」インスタンスが HttpClient に渡されており、ユーザー名とパスワードも HttpPost エンティティに入れていることがわかります。これらのアプローチをそれぞれ個別に試してみましたが、どちらも「エラー」:「不正アクセス」を返しました。結果。
シングルスレッド接続マネージャーを使用する DefaultHttpClient と「ThreadSafeClientConnManager」を試しましたが、どちらも機能しませんでした。