Is it possible login into website programmatically with android? -


i trying login https://thingspeak.com/login website programmatically. have written blow android program not me login. pls tell me doing wrong.

//inside thread function  list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("user id", "****")); namevaluepairs.add(new basicnamevaluepair("password", "*****")); namevaluepairs.add(new basicnamevaluepair("sign in", "sign in")); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost);  //after thread function getting called, below line oncreate method  webview webview = (webview) findviewbyid(r.id.webview); webview.getsettings().setappcacheenabled(false); webview.getsettings().setjavascriptenabled(true); websettings websettings = webview.getsettings(); websettings.setjavascriptenabled(true); webview.loadurl("https://thingspeak.com/channels/42085"); 

i able load website tells channel not public because not logged in. pls me out. in advance.

username:niru

password:helloworld@123

the method searching called http basic authentication. in case target server using ssl gets difficult because need certificate.

such authentication working these steps:

  • login form url.
  • login form data.
  • url authentication.
  • http request / response header.

    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 {      string url = "https://accounts.google.com/serviceloginauth";     string gmail = "https://mail.google.com/mail/";      httpurlconnectionexample http = new httpurlconnectionexample();      // make sure cookies turn on     cookiehandler.setdefault(new cookiemanager());      // 1. send "get" request, can extract form's data.     string page = http.getpagecontent(url);     string postparams = http.getformparams(page, "username@gmail.com", "password");      // 2. construct above post's content , send post request     // authentication     http.sendpost(url, postparams);      // 3. success go gmail.     string result = http.getpagecontent(gmail);     system.out.println(result); }  private void sendpost(string url, string postparams) throws exception {      url obj = new url(url);     conn = (httpsurlconnection) obj.openconnection();      // acts browser     conn.setusecaches(false);     conn.setrequestmethod("post");     conn.setrequestproperty("host", "accounts.google.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", "en-us,en;q=0.5");     (string cookie : this.cookies) {         conn.addrequestproperty("cookie", cookie.split(";", 1)[0]);     }     conn.setrequestproperty("connection", "keep-alive");     conn.setrequestproperty("referer", "https://accounts.google.com/serviceloginauth");     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 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());  }  private string getpagecontent(string url) throws exception {      url obj = new url(url);     conn = (httpsurlconnection) obj.openconnection();      // default     conn.setrequestmethod("get");      conn.setusecaches(false);      // act 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", "en-us,en;q=0.5");     if (cookies != null) {         (string cookie : this.cookies) {             conn.addrequestproperty("cookie", cookie.split(";", 1)[0]);         }     }     int responsecode = conn.getresponsecode();     system.out.println("\nsending 'get' request 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();      // response cookies     setcookies(conn.getheaderfields().get("set-cookie"));      return response.tostring();  }  public string getformparams(string html, string username, string password)         throws unsupportedencodingexception {      system.out.println("extracting form's data...");      document doc = jsoup.parse(html);      // google form id     element loginform = doc.getelementbyid("gaia_loginform");     elements inputelements = loginform.getelementsbytag("input");     list<string> paramlist = new arraylist<string>();     (element inputelement : inputelements) {         string key = inputelement.attr("name");         string value = inputelement.attr("value");          if (key.equals("email"))             value = username;         else if (key.equals("passwd"))             value = password;         paramlist.add(key + "=" + urlencoder.encode(value, "utf-8"));     }      // build parameters list     stringbuilder result = new stringbuilder();     (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; } } 

important resource:
how automate login website – java example


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -