用httpclient:java编写的curl等效代码

时间:2011-03-02 20:48:13

标签: java curl httpclient

有一个在tomcat中运行的web应用程序,基于我的web应用程序中的某些操作,我需要编写一个触发curl的java代码(http://curl.haxx.se)。然后,Curl将查询第三方应用程序并返回XML / JSON。

这必须由我阅读,并向用户返回适当的回复。

我知道这可以通过curl完成,但使用命令行工具从Web应用程序发出请求并不是最好的方法,因此我用Java编写了一个代码,httpclient API。

卷曲代码是

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

curl使用base64编码。因此,我写的Java中的相应代码是

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();  }     }   }
}

使用这个我得到rCode的返回值为“406”。为什么我收到“不可接受”的回复任何有助于我更好地调试并解决此问题的回复。

1 个答案:

答案 0 :(得分:0)

考虑使用记录请求和响应的代理。像VisualProxy这样的东西会做。当我需要时,我没有使用它,因为我写了自己的。我只是将请求写成了页面正文。