2011-06-22 41 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類的主題,但其他的建議當然是非常歡迎:)

編輯更多:我想我要澄清一點。在登錄表單的第一個請求中設置了用戶名和密碼。服務器返回一組cookie,然後將其交給發生實際文件下載的第二個請求。該代碼實際上是PHP的一個工作示例(雖然是匿名的),我的具體問題是在請求之間處理cookie。在cUrl中,所有這些cookie都會自動發生。 謝謝!

+0

對不起,誤解了你的問題。 –

回答

2

你想要結合HttpPost,HttpGet和HttpResponse對象使用HttpClient對象。只看一個例子可能更容易。

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); 

假設登錄成功,現在你可以在執行他們雖然HttpClient的,你可以通過執行登錄執行GET和POST請求作爲身份驗證的用戶,只要。這是管理cookie的這個對象。希望這可以幫助!

編輯:忘了提,你當然可以使用HttpRespose對象來執行錯誤檢查。

+0

感謝您的示例。我現在就試試看。 – marlar

+0

它迄今爲止的作品! Cookie被管理並且服務器響應正確。我繼續: String url =「http://example.com/files/myfile.mp3」; HttpGet get = new 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();