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

查看:138
本文介绍了如何下载一个文件,登录使用的HttpURLConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的Andr​​oid应用程序,我需要从一个网站需要登录下载文件。这不是基本的HTTP认证,而是形式的模拟。

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.

作为新的Andr​​oid我真的不知道如何开始,但在PHP中很容易与卷曲:

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

我会AP preciate在Android中得到这个工作的任何帮助。

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

注:我提的HttpURLConnection的主题,但其他的建议当然是非常欢迎:)

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

编辑:我想我应该澄清一下。在第一请求到登录形成的用户名和密码被设置。服务器返回了一套饼干,然后移交到实际的文件下载发生在第二个请求。在code实际上是从PHP(虽然是匿名的),我的具体问题是处理请求之间的饼干的工作示例。在袅袅所有这些饼干的东西会自动发生。 谢谢!

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!

推荐答案

您会想结合使用的HttpClient的对象HttpPost,HTTPGET,和的Htt presponse对象。这可能是更容易只是看一个例子。

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

假设登录成功,现在就可以,只要你执行它们虽然HttpClient的,你可以通过执行登录执行GET和POST请求作为经过身份验证的用户。正是这种对象管理的cookies。希望这有助于!

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!

编辑:忘了提,你当然可以使用的Htt prespose对象执行错误检查

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

这篇关于如何下载一个文件,登录使用的HttpURLConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆