Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. protected String doInBackground(String... params) {
  2. String type = params[0];
  3. String login_url = "https://www.example.center/api/login";
  4. String login2 = "http://www.example2.win/index.php?action=login";
  5. if (type.equals("login")) {
  6. try {
  7. String email = params[1];
  8. String password = params[2];
  9. URL url = new URL(login_url);
  10. URL url2 = new URL(login2);
  11. final String COOKIES_HEADER = "Set-Cookie";
  12. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  13. HttpURLConnection httpURLConnection2 = (HttpURLConnection) url2.openConnection();
  14. java.net.CookieManager msCookieManager = new java.net.CookieManager();
  15. httpURLConnection.setDoOutput(true);
  16. httpURLConnection.setDoInput(true);
  17. httpURLConnection2.setDoOutput(true);
  18. httpURLConnection2.setDoInput(true);
  19. OutputStream outputStream = httpURLConnection.getOutputStream();
  20. OutputStream outputStream2 = httpURLConnection2.getOutputStream();
  21. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  22. String post_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&"
  23. + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  24. BufferedWriter bufferedWriter2 = new BufferedWriter(new OutputStreamWriter(outputStream2, "UTF-8"));
  25. String post_data2 = URLEncoder.encode("myusername", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&"
  26. + URLEncoder.encode("mypassword", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
  27. bufferedWriter.write(post_data);
  28. bufferedWriter2.write(post_data2);
  29. bufferedWriter.flush();
  30. bufferedWriter.close();
  31. bufferedWriter2.flush();
  32. bufferedWriter2.close();
  33. outputStream.close();
  34. outputStream2.close();
  35. InputStream inputStream = httpURLConnection.getInputStream();
  36. InputStream inputStream2 = httpURLConnection2.getInputStream();
  37. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  38. BufferedReader bufferedReader2 = new BufferedReader(new InputStreamReader(inputStream2, "iso-8859-1"));
  39. Map<String, List<String>> headerFields = httpURLConnection2.getHeaderFields();
  40. List<String> cookiesHeader = headerFields.get(COOKIES_HEADER);
  41.  
  42. if (cookiesHeader != null) {
  43. for ( String cookie : cookiesHeader) {
  44. msCookieManager.getCookieStore().add(null, HttpCookie.parse(cookie).get(0));
  45. cookies = cookie;
  46. }
  47.  
  48. }
  49. while ((line = bufferedReader.readLine()) != null) {
  50. result += line;
  51. }
  52. bufferedReader.close();
  53. bufferedReader2.close();
  54. inputStream.close();
  55. inputStream2.close();
  56. httpURLConnection.disconnect();
  57. httpURLConnection2.disconnect();
  58. JSONObject obj= null;
  59. try {
  60. obj = new JSONObject(result);
  61. String status=obj.getString("status");
  62. JSONObject data_obj=obj.getJSONObject("data");
  63. win_user=data_obj.getString("id");
  64. name=data_obj.getString("name");
  65. logo=data_obj.getString("logo");
  66.  
  67. return win_user;
  68. } catch (JSONException e) {
  69. e.printStackTrace();
  70. }
  71. return result;
  72.  
  73.  
  74. } catch (MalformedURLException e) {
  75. e.printStackTrace();
  76. } catch (IOException e) {
  77. e.printStackTrace();}
  78. }
  79. return null;
  80. }
  81.  
  82. @Override
  83. protected void onPreExecute() {
  84. alertDialog = new AlertDialog.Builder(context).create();
  85. alertDialog.setTitle("Login Status");
  86. }
  87.  
  88.  
  89. @Override
  90. protected void onPostExecute(String result) {
  91. if (result == null ) {
  92. Toast pass = Toast.makeText(context, "Email or Password is not match!", Toast.LENGTH_SHORT);
  93. pass.show();
  94. }
  95. else
  96. {
  97. context.startActivity( new Intent(context,Main.class));
  98. Main.JSON_STRING = result;
  99. Main.name = name;
  100. Main.logo = logo;
  101. Main.win_user = win_user;
  102. Main.cookie = cookies;
  103. Toast pass = Toast.makeText(context, cookies, Toast.LENGTH_LONG);
  104. pass.show();
  105. }
  106.  
  107. }
  108.  
  109. @Override
  110. protected void onProgressUpdate(Void... values) {
  111. super.onProgressUpdate(values);
  112. }
  113. }
  114.  
  115. Set-Cookie:PHPSESSID=....;path=/
  116. Set-Cookie:usrnm......;expires.....;path=/;domain=www.example2.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement