How to upload a login file using HttpURLConnection

For my Android application, I need to download a file from a site that requires a login. This is not basic HTTP authentication, but rather emulation.

Being new to Android, I really don't know how to get started, but in PHP it's 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 doing this on Android.

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

Edit: I think I should clarify a bit. The first request for the registration form sets the username and password. The server returns a set of cookies, which are then passed to the second request, where the actual download of the file takes place. The code is actually a working example from PHP (albeit anonymous), and my specific problem is handling cookies between requests. In cUrl, all these cookies occur automatically. Thanks!

+1
source share
2 answers

You will want to use the HttpClient object in conjunction with the HttpPost, HttpGet, and HttpResponse objects. It's 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 if you execute them, although the HttpClient from which you are logged in. It is this object that controls cookies. Hope this helps!

EDIT: Forgot to mention that you can, of course, use the HttpRespose object to check for errors.

+2
source

This is what I came up with with the help of the S201 answer, as well as a lot of search queries. The code is simplified without try-catch constructs.

 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(); 
+4
source

Source: https://habr.com/ru/post/919060/


All Articles