curl equivalent code written in httpclient : java -


have web-application running in tomcat, based on actions in web-applications need write java code trigger curl (http://curl.haxx.se). curl query third-party application , return xml/json.

this has read me , return appropriate response user.

i know can done curl using command line tool making request web application not best way go , have written code in java, httpclient api.

the curl code was

curl -u username:password -d "param1=aaa& param2=bbb" -k http://www.testme.com/api/searches.xml 

curl default uses base64 encoding. corresponding code in java written me is

import org.apache.commons.codec.binary.base64; import org.apache.commons.httpclient.httpclient; import org.apache.commons.httpclient.httpstatus; import org.apache.commons.httpclient.usernamepasswordcredentials; import org.apache.commons.httpclient.methods.postmethod;  import java.io.bufferedreader; import java.io.bytearrayoutputstream; import java.io.inputstreamreader;  public class ncs2 {    public static void main(string args[]) {      string username = "abc";     string password = "xyz";      httpclient httpclient = new httpclient();     bufferedreader bufferedreader = null;       postmethod postmethod = new postmethod("https://www.testabc.com/api/searches.xml");     postmethod.addparameter("_def_id","8");     postmethod.addparameter("dobday", "6");     postmethod.addparameter("dobmonth","6");     postmethod.addparameter("dobyear", "1960");     postmethod.addparameter("firstname", "test");     postmethod.addparameter("lastname", "test");       string username_encoded = new string(base64.encodebase64(username.getbytes()));     system.out.println("username_encoded ="+username_encoded);      string password_encoded = new string(base64.encodebase64(password.getbytes()));     system.out.println("password_encoded ="+password_encoded);      httpclient.getstate().setauthenticationpreemptive(true);     usernamepasswordcredentials credentials = new usernamepasswordcredentials();     credentials.setpassword(username_encoded);     credentials.setusername(password_encoded);   httpclient.getstate().setcredentials("form","http://www.testabc.com/api/searches.xml",credentials);   // not sure 1 use here..      try{           int rcode = httpclient.executemethod(postmethod);           system.out.println("rcode is" +rcode);            if(rcode == httpstatus.sc_not_implemented)             {               system.err.println("the post postmethod not implemented uri");                postmethod.getresponsebodyasstring();            }               else if(rcode == httpstatus.sc_not_acceptable) {                 system.out.println(postmethod.getresponsebodyasstring());           }            else {                       bufferedreader = new bufferedreader(new inputstreamreader(postmethod.getresponsebodyasstream()));                        string readline;         while(((readline = bufferedreader.readline()) != null)) {           system.out.println("return value " +readline);       }       }     } catch (exception e) {       system.err.println(e);     } {       postmethod.releaseconnection();       if(bufferedreader != null) try { bufferedreader.close(); } catch (exception fe)    fe.printstacktrace();  }     }   } } 

using return value rcode "406". why receiving response "not acceptable" me debug better , fix this.

consider using proxy logs request , response. visualproxy do. haven't used wrote own when needed one. mine wrote request out body of page.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -