2011-03-02 42 views
2

有一個在tomcat中運行的web應用程序,基於我的web應用程序中的某些動作,我需要編寫一個可以觸發curl的java代碼(http://curl.haxx.se )。然後,Curl將查詢第三方應用程序並返回一個XML/JSON。在httpclient中編寫的curl等價代碼:java

這必須由我自己閱讀並返回適當的響應給用戶。

我知道這可以用curl來完成,但是使用命令行工具從Web應用程序發出請求並不是最好的方式,因此我已經用Java,httpclient API編寫了代碼。

捲曲代碼是

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

捲曲默認使用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」。爲什麼我收到「不可接受」的響應?有助於我更好地調試並解決此問題的任何內容。

回答

0

考慮使用記錄請求和響應的代理。像VisualProxy會做的。我沒有使用它,因爲我需要一個自己寫的。我只是將請求寫成頁面的主體。