开发者

curl equivalent code written in httpclient : java

have a web-application running in tomcat, based on certain actions in my web-applications I need to write a java code which would trigger curl (http://curl.haxx.se). Curl would then query a third-party application and return an XML/JSON.

This has to be read by me and return appropriate response to the user.

I know this can be done with curl but using a command line tool for making request from a web application is not the best way to go about it and thus I have written a 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 by default uses base64 encoding. Thus the corresponding code in Java which is written by 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);   // I am not sure which one to 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 is not implemented by this 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);
    } finally {
      postmethod.releaseConnection();
      if(bufferedreader != null) try { bufferedreader.close(); } catch (Exception fe)    fe.printStackTrace();  }     }   }
}

Using this I get the return value for rCode as "406". Why am I receiving a response which is "Not Acceptable" Anything which would help me debug better and fix this.


Consider using a proxy which logs the request and response. Something like VisualProxy would do. I haven't used it as I wrote my own when I needed one. Mine just wrote the request out as the body of the page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜