Guest User

Untitled

a guest
Oct 5th, 2018
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.00 KB | None | 0 0
  1. package com.example.administrator.instadeneme;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.net.ConnectivityManager;
  6. import android.net.NetworkInfo;
  7.  
  8. import org.jsoup.Jsoup;
  9. import org.jsoup.nodes.Document;
  10. import org.jsoup.nodes.Element;
  11. import org.jsoup.select.Elements;
  12.  
  13. import java.io.BufferedReader;
  14. import java.io.DataOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17. import java.io.UnsupportedEncodingException;
  18. import java.net.CookieHandler;
  19. import java.net.CookieManager;
  20. import java.net.URL;
  21. import java.net.URLEncoder;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24.  
  25. import javax.net.ssl.HttpsURLConnection;
  26.  
  27. public class HttpUrlConnectionExample {
  28. private List<String> cookies;
  29. private HttpsURLConnection conn;
  30.  
  31. private final String USER_AGENT = "Mozilla/5.0";
  32.  
  33. public static void main(String[] args) throws Exception {
  34.  
  35. }
  36.  
  37. public void sendPost(String url, String postParams) throws Exception {
  38.  
  39. URL obj = new URL(url);
  40. conn = (HttpsURLConnection) obj.openConnection();
  41.  
  42. // Acts like a browser
  43. conn.setUseCaches(false);
  44. conn.setRequestMethod("POST");
  45. conn.setRequestProperty("Host", "www.instagram.com");
  46. conn.setRequestProperty("User-Agent", USER_AGENT);
  47. conn.setRequestProperty("Accept",
  48. "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  49. conn.setRequestProperty("Accept-Language", "tr-TR,tr;q=0.9,en-
  50. US;q=0.8,en;q=0.7");
  51. for (String cookie : this.cookies) {
  52. conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
  53. }
  54. conn.setRequestProperty("Connection", "keep-alive");
  55. conn.setRequestProperty("Referer",
  56. "https://www.instagram.com/accounts/login/?force_classic_login");
  57. conn.setRequestProperty("Content-Type", "application/x-www-form-
  58. urlencoded");
  59. conn.setRequestProperty("Content-Length",
  60. Integer.toString(postParams.length()));
  61.  
  62. conn.setDoOutput(true);
  63. conn.setDoInput(true);
  64.  
  65. // Send post request
  66. DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
  67. wr.writeBytes(postParams);
  68. wr.flush();
  69. wr.close();
  70.  
  71. int responseCode = conn.getResponseCode();
  72. System.out.println("nSending 'POST' request to URL : " + url);
  73. System.out.println("Post parameters : " + postParams);
  74. System.out.println("Response Code : " + responseCode);
  75.  
  76. BufferedReader in =
  77. new BufferedReader(new
  78. InputStreamReader(conn.getInputStream()));
  79. String inputLine;
  80. StringBuffer response = new StringBuffer();
  81.  
  82. while ((inputLine = in.readLine()) != null) {
  83. response.append(inputLine);
  84. }
  85. in.close();
  86. // System.out.println(response.toString());
  87.  
  88. }
  89.  
  90. public String GetPageContent(String url) throws Exception {
  91.  
  92. URL obj = new URL(url);
  93. conn = (HttpsURLConnection) obj.openConnection();
  94.  
  95. // default is GET
  96. conn.setRequestMethod("GET");
  97.  
  98. conn.setUseCaches(false);
  99.  
  100. // act like a browser
  101. conn.setRequestProperty("User-Agent", USER_AGENT);
  102. conn.setRequestProperty("Accept",
  103. "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  104. conn.setRequestProperty("Accept-Language", "tr-TR,tr;q=0.9,en-
  105. US;q=0.8,en;q=0.7");
  106. if (cookies != null) {
  107. for (String cookie : this.cookies) {
  108. conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
  109. }
  110. }
  111. int responseCode = conn.getResponseCode();
  112. System.out.println("nSending 'GET' request to URL : " + url);
  113. System.out.println("Response Code : " + responseCode);
  114.  
  115. BufferedReader in =
  116. new BufferedReader(new
  117. InputStreamReader(conn.getInputStream()));
  118. String inputLine;
  119. StringBuffer response = new StringBuffer();
  120.  
  121. while ((inputLine = in.readLine()) != null) {
  122. response.append(inputLine);
  123. }
  124. in.close();
  125.  
  126. // Get the response cookies
  127. setCookies(conn.getHeaderFields().get("Set-Cookie"));
  128.  
  129. return response.toString();
  130.  
  131. }
  132.  
  133. public String getFormParams(String html, String username, String password)
  134. throws IOException {
  135.  
  136. System.out.println("Extracting form's data...");
  137.  
  138.  
  139. Document doc = Jsoup.parse(html);
  140. //Document doc=Jsoup.connect("https://www.instagram.com/accounts/login/?
  141. force_classic_login").get();
  142. // Google form id
  143.  
  144.  
  145.  
  146. Element loginform = doc.getElementById("login-form");
  147. Elements inputElements = loginform.getElementsByTag("input");
  148. List<String> paramList = new ArrayList<String>();
  149. for (Element inputElement : inputElements) {
  150. String key = inputElement.attr("name");
  151. String value = inputElement.attr("value");
  152.  
  153. if (key.equals("id_username"))
  154. value = username;
  155. else if (key.equals("id_password"))
  156. value = password;
  157. paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
  158. }
  159.  
  160. // build parameters list
  161. StringBuilder result = new StringBuilder();
  162. for (String param : paramList) {
  163. if (result.length() == 0) {
  164. result.append(param);
  165. } else {
  166. result.append("&" + param);
  167. }
  168. }
  169. return result.toString();
  170. }
  171.  
  172. public List<String> getCookies() {
  173. return cookies;
  174. }
  175.  
  176. public void setCookies(List<String> cookies) {
  177. this.cookies = cookies;
  178. }
  179. }
  180.  
  181. package com.example.administrator.instadeneme;
  182.  
  183. import android.app.ProgressDialog;
  184. import android.content.Context;
  185. import android.net.ConnectivityManager;
  186. import android.net.NetworkInfo;
  187. import android.os.AsyncTask;
  188. import android.os.StrictMode;
  189. import android.support.v7.app.AppCompatActivity;
  190. import android.os.Bundle;
  191. import android.view.View;
  192. import android.webkit.WebView;
  193. import android.webkit.WebViewClient;
  194. import android.widget.Button;
  195. import android.widget.EditText;
  196.  
  197. import org.jsoup.Jsoup;
  198. import org.jsoup.nodes.Document;
  199.  
  200. import java.io.IOException;
  201. import java.io.UnsupportedEncodingException;
  202. import java.net.CookieHandler;
  203. import java.net.CookieManager;
  204.  
  205.  
  206. public class MainActivity extends AppCompatActivity
  207. {
  208. Button butonCon;
  209. EditText textUserName;
  210. EditText textPassword;
  211. WebView web;
  212. WebViewClient mWeb;
  213.  
  214.  
  215. @Override
  216. protected void onCreate(Bundle savedInstanceState) {
  217. StrictMode.ThreadPolicy policy = new
  218. StrictMode.ThreadPolicy.Builder().permitAll().build();
  219. StrictMode.setThreadPolicy(policy);
  220.  
  221. super.onCreate(savedInstanceState);
  222. setContentView(R.layout.activity_main);
  223. web=findViewById(R.id.webIns);
  224. textUserName=findViewById(R.id.et_kullaniciAdi);
  225. textPassword=findViewById(R.id.et_sifre);
  226. butonCon=findViewById(R.id.bt_connect);
  227. butonCon.setOnClickListener(new View.OnClickListener() {
  228. @Override
  229. public void onClick(View v)
  230. {
  231. String url = "https://www.instagram.com/accounts/login/?
  232. force_classic_login";
  233. String instagram = "https://www.instagram.com/";
  234.  
  235. HttpUrlConnectionExample http = new HttpUrlConnectionExample();
  236.  
  237. // make sure cookies is turn on
  238. CookieHandler.setDefault(new CookieManager());
  239.  
  240. // 1. Send a "GET" request, so that you can extract the form's
  241. data.
  242. String page = null;
  243. try {
  244. page = http.GetPageContent(url);
  245. } catch (Exception e) {
  246. e.printStackTrace();
  247. }
  248. String postParams = null;
  249. try {
  250. postParams = http.getFormParams(page,textUserName.getText().
  251. toString(),textPassword.getText().toString());
  252.  
  253. } catch (UnsupportedEncodingException e) {
  254. e.printStackTrace();
  255. } catch (IOException e) {
  256. e.printStackTrace();
  257. }
  258.  
  259. // 2. Construct above post's content and then send a POST
  260. request for
  261. // authentication
  262. try {
  263. http.sendPost(url, postParams);
  264. } catch (Exception e) {
  265. e.printStackTrace();
  266. }
  267.  
  268. // 3. success then go to gmail.
  269. String result = null;
  270. try {
  271. result = http.GetPageContent(instagram);
  272. } catch (Exception e) {
  273. e.printStackTrace();
  274. }
  275. System.out.println(result);
  276.  
  277. }
  278. });
  279.  
  280. }
  281. }
Add Comment
Please, Sign In to add comment