Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.administrator.instadeneme;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.CookieHandler;
- import java.net.CookieManager;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.List;
- import javax.net.ssl.HttpsURLConnection;
- public class HttpUrlConnectionExample {
- private List<String> cookies;
- private HttpsURLConnection conn;
- private final String USER_AGENT = "Mozilla/5.0";
- public static void main(String[] args) throws Exception {
- }
- public void sendPost(String url, String postParams) throws Exception {
- URL obj = new URL(url);
- conn = (HttpsURLConnection) obj.openConnection();
- // Acts like a browser
- conn.setUseCaches(false);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Host", "www.instagram.com");
- conn.setRequestProperty("User-Agent", USER_AGENT);
- conn.setRequestProperty("Accept",
- "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
- conn.setRequestProperty("Accept-Language", "tr-TR,tr;q=0.9,en-
- US;q=0.8,en;q=0.7");
- for (String cookie : this.cookies) {
- conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
- }
- conn.setRequestProperty("Connection", "keep-alive");
- conn.setRequestProperty("Referer",
- "https://www.instagram.com/accounts/login/?force_classic_login");
- conn.setRequestProperty("Content-Type", "application/x-www-form-
- urlencoded");
- conn.setRequestProperty("Content-Length",
- Integer.toString(postParams.length()));
- conn.setDoOutput(true);
- conn.setDoInput(true);
- // Send post request
- DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
- wr.writeBytes(postParams);
- wr.flush();
- wr.close();
- int responseCode = conn.getResponseCode();
- System.out.println("nSending 'POST' request to URL : " + url);
- System.out.println("Post parameters : " + postParams);
- System.out.println("Response Code : " + responseCode);
- BufferedReader in =
- new BufferedReader(new
- InputStreamReader(conn.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- // System.out.println(response.toString());
- }
- public String GetPageContent(String url) throws Exception {
- URL obj = new URL(url);
- conn = (HttpsURLConnection) obj.openConnection();
- // default is GET
- conn.setRequestMethod("GET");
- conn.setUseCaches(false);
- // act like a browser
- conn.setRequestProperty("User-Agent", USER_AGENT);
- conn.setRequestProperty("Accept",
- "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
- conn.setRequestProperty("Accept-Language", "tr-TR,tr;q=0.9,en-
- US;q=0.8,en;q=0.7");
- if (cookies != null) {
- for (String cookie : this.cookies) {
- conn.addRequestProperty("Cookie", cookie.split(";", 1)[0]);
- }
- }
- int responseCode = conn.getResponseCode();
- System.out.println("nSending 'GET' request to URL : " + url);
- System.out.println("Response Code : " + responseCode);
- BufferedReader in =
- new BufferedReader(new
- InputStreamReader(conn.getInputStream()));
- String inputLine;
- StringBuffer response = new StringBuffer();
- while ((inputLine = in.readLine()) != null) {
- response.append(inputLine);
- }
- in.close();
- // Get the response cookies
- setCookies(conn.getHeaderFields().get("Set-Cookie"));
- return response.toString();
- }
- public String getFormParams(String html, String username, String password)
- throws IOException {
- System.out.println("Extracting form's data...");
- Document doc = Jsoup.parse(html);
- //Document doc=Jsoup.connect("https://www.instagram.com/accounts/login/?
- force_classic_login").get();
- // Google form id
- Element loginform = doc.getElementById("login-form");
- Elements inputElements = loginform.getElementsByTag("input");
- List<String> paramList = new ArrayList<String>();
- for (Element inputElement : inputElements) {
- String key = inputElement.attr("name");
- String value = inputElement.attr("value");
- if (key.equals("id_username"))
- value = username;
- else if (key.equals("id_password"))
- value = password;
- paramList.add(key + "=" + URLEncoder.encode(value, "UTF-8"));
- }
- // build parameters list
- StringBuilder result = new StringBuilder();
- for (String param : paramList) {
- if (result.length() == 0) {
- result.append(param);
- } else {
- result.append("&" + param);
- }
- }
- return result.toString();
- }
- public List<String> getCookies() {
- return cookies;
- }
- public void setCookies(List<String> cookies) {
- this.cookies = cookies;
- }
- }
- package com.example.administrator.instadeneme;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.os.AsyncTask;
- import android.os.StrictMode;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.webkit.WebView;
- import android.webkit.WebViewClient;
- import android.widget.Button;
- import android.widget.EditText;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.CookieHandler;
- import java.net.CookieManager;
- public class MainActivity extends AppCompatActivity
- {
- Button butonCon;
- EditText textUserName;
- EditText textPassword;
- WebView web;
- WebViewClient mWeb;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- StrictMode.ThreadPolicy policy = new
- StrictMode.ThreadPolicy.Builder().permitAll().build();
- StrictMode.setThreadPolicy(policy);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- web=findViewById(R.id.webIns);
- textUserName=findViewById(R.id.et_kullaniciAdi);
- textPassword=findViewById(R.id.et_sifre);
- butonCon=findViewById(R.id.bt_connect);
- butonCon.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v)
- {
- String url = "https://www.instagram.com/accounts/login/?
- force_classic_login";
- String instagram = "https://www.instagram.com/";
- HttpUrlConnectionExample http = new HttpUrlConnectionExample();
- // make sure cookies is turn on
- CookieHandler.setDefault(new CookieManager());
- // 1. Send a "GET" request, so that you can extract the form's
- data.
- String page = null;
- try {
- page = http.GetPageContent(url);
- } catch (Exception e) {
- e.printStackTrace();
- }
- String postParams = null;
- try {
- postParams = http.getFormParams(page,textUserName.getText().
- toString(),textPassword.getText().toString());
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- // 2. Construct above post's content and then send a POST
- request for
- // authentication
- try {
- http.sendPost(url, postParams);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 3. success then go to gmail.
- String result = null;
- try {
- result = http.GetPageContent(instagram);
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println(result);
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment