java - How to extract the URL following an HTTPPost in Android -


i'm scratching head @ problem can't seem figure out. i'm trying extract url following httppost. allow me go through oauth process.

currently i'm extracting whole website entity.

for example: data posted https://mysite.com/login , redirect after post https://mysite.com/dashboard?code=3593085390859082093720

how 1 extract url?

if need more information or can direct me in right direction, appreciated! thank you!

import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.list;  import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.httpclient; import org.apache.http.client.responsehandler; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.basicresponsehandler; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair;  import android.app.activity; import android.content.intent; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast;  public class loginactivity extends activity implements onclicklistener {  button ok,back,exit; textview result;  /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.login);      // login button clicked     ok = (button)findviewbyid(r.id.submit);     ok.setonclicklistener(this);      result = (textview)findviewbyid(r.id.result);  }  public void postlogindata() {     // create new httpclient , post header     httpclient httpclient = new defaulthttpclient();      httppost httppost = new httppost("https://mysite.com/login");      try {         // add user name , password      edittext uname = (edittext)findviewbyid(r.id.username);      string username = uname.gettext().tostring();       edittext pword = (edittext)findviewbyid(r.id.password);      string password = pword.gettext().tostring();          list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2);         namevaluepairs.add(new basicnamevaluepair("pseudonym_session[unique_id]", username));         namevaluepairs.add(new basicnamevaluepair("pseudonym_session[password]", password));         httppost.setentity(new urlencodedformentity(namevaluepairs));          // execute http post request         log.w("canvas", "execute http post request");         httpresponse response = httpclient.execute(httppost);          string str = inputstreamtostring(response.getentity().getcontent()).tostring();         log.w("canvas", str);          responsehandler<string> responsehandler = new basicresponsehandler();          string responsebody = httpclient.execute(httppost, responsehandler);          intent intent = new intent(getbasecontext(), dashboardactivity.class);         startactivity(intent);          if(str.tostring().equalsignorecase("true"))         {          log.w("canvas", "true");          result.settext("login successful");            }else         {          log.w("canvas", "false");          result.settext(responsebody);          }      } catch (clientprotocolexception e) {      e.printstacktrace();     } catch (ioexception e) {      e.printstacktrace();     } }   private stringbuilder inputstreamtostring(inputstream is) {  string line = "";  stringbuilder total = new stringbuilder();  // wrap bufferedreader around inputstream  bufferedreader rd = new bufferedreader(new inputstreamreader(is));  // read response until end  try {   while ((line = rd.readline()) != null) {      total.append(line);    }  } catch (ioexception e) {   e.printstacktrace();  }  // return full string  return total; }  @override public void onclick(view view) {   if(view == ok){     postlogindata();   } }  } 

if set redirect handler, can response location server's sending to. here's snippet of code playing with... (and should point out, if don't set redirect handler, you'll redirected final destination, might login screen itself)

defaulthttpclient htc = gethttpclient(); htc.setredirecthandler(new redirecthandler() {   @override   public boolean isredirectrequested(httpresponse response, httpcontext context)   {     log.d(tag, "isredirectrequested, response: " + response.tostring());     return false;   }    @override   public uri getlocationuri(httpresponse response, httpcontext context)       throws protocolexception   {     log.d(tag, "getlocationuri, response: " + response.tostring());     return null;   } }); httpresponse resp = null; stringbuilder out = new stringbuilder(); try {   httpget = new httpget(spec);   resp = htc.execute(get);   (header hdr : resp.getallheaders())     log.d(tag, "header " + hdr.getname() + " -> " + hdr.getvalue());   ... } catch (exception e) {   log.e(tag, "error connecting " + spec, e);   return null; } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -