【问题标题】:curl equivalent code written in httpclient : java用 httpclient 编写的 curl 等效代码:java
【发布时间】:2011-03-02 20:48:13
【问题描述】:

有一个 web 应用程序在 tomcat 中运行,基于我的 web 应用程序中的某些操作,我需要编写一个 java 代码来触发 curl (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”。为什么我会收到“不可接受”的响应任何可以帮助我更好地调试和解决此问题的内容。

【问题讨论】:

    标签: java curl httpclient


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多