根据我的Web应用程序中的某些操作,有一个在Tomcat中运行的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”。为什么我会收到“不可接受”的答复,这将有助于我更好地调试并解决此问题。

最佳答案

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

关于java - curl用httpclient编写的等效代码:java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5173251/

10-11 22:15
查看更多