如何使用HttpURLConnection下载一个带有登录的文件[英] How do download a file with login using HttpURLConnection

本文是小编为大家收集整理的关于如何使用HttpURLConnection下载一个带有登录的文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

对于我的Android应用程序,我需要从需要登录的站点下载文件.它不是基本的HTTP身份验证,而是仿真.

是Android的新手,我真的不知道该如何入门,但是在PHP中,Curl很容易:

// 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.在卷发中,所有这些cookie的东西会自动发生. 谢谢!

推荐答案

您将要与HTTPPPOST,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,就可以执行并发布作为身份验证的用户的获取和发布请求.正是这个对象管理cookie.希望这会有所帮助!

编辑:忘了提到您当然可以使用httprose对象执行错误检查.

其他推荐答案

这是我提出的S201答案和大量谷歌搜索的帮助.该代码被简化,没有试用构造.

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

本文地址:https://www.itbaoku.cn/post/102695.html

问题描述

For my Android app I need to download a file from a site requiring login. It is not basic http authentication but rather form emulation.

Being new to Android I really don't know how to get started, but in PHP it is easy with cUrl:

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

I would appreciate any help in getting this done in Android.

Note: I mention HttpURLConnection in the subject, but other suggestions are of course more than welcome :)

Edit: I think I should clarify a bit. In the first request to the login form the username and password is set. The server returns a set of cookies which is then handed over to the second request where the actual file download takes place. The code is actually a working example from PHP (albeit anonymized) and my specific problem is handling the cookies between the requests. In cUrl all this cookie stuff happens automatically. Thanks!

推荐答案

You going to want to use an HttpClient object in combination with HttpPost, HttpGet, and HttpResponse objects. It is probably easier to just look at an example.

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

Assuming the login was successful, you can now execute GET and POST requests as an authenticated user as long as you execute them though the HttpClient that you executed the login through. It is this object that manages the cookies. Hope this helps!

EDIT: Forgot to mention that you can of course use the HttpRespose object to perform error checking.

其他推荐答案

This is what I came up with helped by S201's answer plus a lot of googling. The code is simplified and without try-catch constructions.

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