2011-06-22 37 views
1

私のAndroidアプリでは、ログインが必要なサイトからファイルをダウンロードする必要があります。基本的なHTTP認証ではなく、むしろエミュレーションを形成します。HttpURLConnectionを使用してログインしてファイルをダウンロードするには

は、Androidに新たなので、私は本当に開始する方法を知りませんが、PHPでそれはカールと簡単です:

// Set standard options 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3"); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "somecookiejar"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "somecookiefile"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

// Login 
curl_setopt($ch, CURLOPT_URL, "https://example.com"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=myusername&password=mypassword"); 
curl_exec($ch); 

// Get the file 
curl_setopt($ch, CURLOPT_URL, "http://example.com/files/myfile.mp3"); 
curl_setopt($ch, CURLOPT_POST, 0); 
writefile(curl_exec($ch), "myfile.mp3"); 
curl_close($ch); 

私は、これはAndroidの中で行われ得ることで任意の助けをいただければ幸いです。

注:私は、件名にHttpURLConnectionのは言うまでもなく、他の提案はもちろん歓迎以上です:)

編集:私は、私は少しを明確にすべきだと思います。ログインフォームへの最初の要求では、ユーザー名とパスワードが設定されます。サーバは、実際のファイルのダウンロードが行われる第2の要求に引き渡されるクッキーのセットを返す。このコードは実際にはPHPの実例です(匿名化されていますが)。私の具体的な問題はリクエスト間のクッキーを処理することです。 cUrlでは、このクッキーはすべて自動的に発生します。 ありがとう!

+0

ご迷惑をおかけして申し訳ございません。 –

答えて

2

HttpClientオブジェクトをHttpPost、HttpGet、およびHttpResponseオブジェクトと組み合わせて使用​​する場合は、おそらく例を見るほうが簡単でしょう。

ログインが成功した、あなたは今、限り、あなたはあなたを介してログインを実行していることのHttpClientかかわらず、それらを実行すると、認証されたユーザーとしてGETとPOSTリクエストを実行することができますと仮定
HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(context.getString(R.string.loginURL)); 
HttpResponse response = null; 
List<NameValuePair> postFields = new ArrayList<NameValuePair>(2); 

// Set the post fields 
postFields.add(new BasicNameValuePair("username", settings.getString("username", "null"))); 
postFields.add(new BasicNameValuePair("password", settings.getString("password", "null"))); 
post.setEntity(new UrlEncodedFormEntity(postFields, HTTP.UTF_8)); 

// Execute the POST request 
response = client.execute(post); 

。クッキーを管理するのはこのオブジェクトです。お役に立てれば!

編集:勿論、HttpResposeオブジェクトを使用してエラーチェックを行うことができます。

+0

この例をありがとう。私は今試してみる。 – marlar

+0

これまでのところ動作します!クッキーは管理されており、サーバーの応答は正しいです。私は続けます: String url = "http://example.com/files/myfile.mp3"; HttpGet get =新しいHttpGet(url); response = client.execute(get); しかし、実際にファイルのバイナリデータを取得するにはどうすればよいですか?言い換えれば、どのようにGET要求からコンテンツを取り出すのですか? – marlar

+0

最後に私はあなたが提供した基本情報のおかげでそれを得ました!私は新しい答えで自分のコードを追加しました。私はこれが私のコードを追加する適切な方法であるかどうか、私は元の質問を更新しなければならないかどうかはわかりません。 – marlar

4

これは、私がS201の答えと多くのグーグルーグの助けを借りてきたことです。コードは単純化され、try-catch構造はありません。

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost("https://example.com/login"); 
HttpResponse response = null; 
List<NameValuePair> postFields = new ArrayList<NameValuePair>(2); 

// Set the post fields 
postFields.add(new BasicNameValuePair("username", "myusername")); 
postFields.add(new BasicNameValuePair("password", "mypassword")); 
post.setEntity(new UrlEncodedFormEntity(postFields, HTTP.UTF_8)); 

// Execute the POST request 
response = client.execute(post); 

// Now GET the file 
HttpGet get = new HttpGet("http://example.com/files/myfile.mp3"); 
response = client.execute(get); 

HttpEntity entity = response.getEntity(); 
InputStream in = entity.getContent(); 

// Save the file to SD 
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 
path.mkdirs(); 
File file = new File(path, "myfile.mp3"); 
FileOutputStream fos = new FileOutputStream(file); 

byte[] buffer = new byte[1024]; 
int len1 = 0; 
while ((len1 = in.read(buffer)) > 0) { 
     fos.write(buffer, 0, len1); 
} 

fos.close(); 
関連する問題