Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. public class RequestHandler {
  2.     public String postRequest(String hurl, String mUsername, String mPassword)
  3.     {
  4.         OutputStreamWriter request;
  5.  
  6.         HttpURLConnection post_connection;
  7.  
  8.         URL url;
  9.         String response = null;
  10.         String parameters = "UserName="+mUsername+"&Password="+mPassword;
  11.  
  12.         try
  13.         {
  14.             url = new URL(hurl);
  15.             post_connection = (HttpURLConnection) url.openConnection();
  16.             post_connection.setDoOutput(true);
  17.             post_connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  18.             post_connection.setRequestMethod("POST");
  19.  
  20.             request = new OutputStreamWriter(post_connection.getOutputStream());
  21.             request.write(parameters);
  22.             request.flush();
  23.             request.close();
  24.             String line = "";
  25.             InputStreamReader isr = new InputStreamReader(post_connection.getInputStream());
  26.             BufferedReader reader = new BufferedReader(isr);
  27.             StringBuilder sb = new StringBuilder();
  28.             while ((line = reader.readLine()) != null)
  29.             {
  30.                 sb.append(line).append("\n");
  31.             }
  32.  
  33.             response = sb.toString();
  34.  
  35.             Log.d(LOG_TAG, response);
  36.             Log.d(LOG_TAG, "Code: " + post_connection.getResponseCode());
  37.             isr.close();
  38.             reader.close();
  39.         }
  40.         catch(IOException e)
  41.         {
  42.             Log.e(LOG_TAG, e.toString());
  43.         }
  44.         return response;
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement