Jackie Jackie - 1 year ago 174
Android Question

How to see full HttpPost Request using Apache HttpClient on Android

So I have code that contstructs a HttpPost request like the following...

public LoginForm apa;
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https",
SSLSocketFactory.getSocketFactory(), 443));
HttpParams httpparams = new BasicHttpParams();
SingleClientConnManager mgr = new SingleClientConnManager(httpparams, schemeRegistry);
HttpClient httpclient = new DefaultHttpClient(mgr, httpparams);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for(String i : apa.getParams().keySet()){
nameValuePairs.add(new BasicNameValuePair(i, apa.getParams().get(i)));
List<NameValuePair> cookies = new ArrayList<NameValuePair>();
StringBuilder sb = new StringBuilder();
for(String i : apa.getCookies().keySet()){
// Trying to remove last ;
String cookie = sb.toString();
cookie = cookie.substring(0, cookie.length()-1);
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded");
httppost.setHeader("Cookie", cookie);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);

It isn't logging in quite as I would expect so is there a way to output the entire request (headers and all) to match it up with the one I see in my Chrome Dev Tools? Am I over thinking this? Is there a form login library for Android or Java in general? Do I need content-length added?

Answer Source

Printing the headers is easy - you can list them using method getAllHeaders. If you need to print HttpEntity, you can use method writeTo to write whole entity into ByteArrayOutputStream and create a String using toString.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download