2010-08-05 57 views
1

有沒有人有關於如何編寫java或javafx cURL應用程序的好教程?我已經看到大量關於如何啓動外部調用的教程,如XML文件,但是我試圖檢索XML提要,以便在能夠檢索XML提要之前提交用戶名和密碼。使用Java的cURL應用程序

回答

2

你想完成什麼?您是否嘗試通過HTTP檢索XML Feed?

在這種情況下,我建議你看看Apache HttpClient。它以cURL的形式提供相似的功能,但是採用純Java方式(cURL是本地C應用程序)。 HttpClient支持多個authentication mechanisms。例如,您可以使用基本身份驗證這樣提交的用戶名/密碼:

public static void main(String[] args) throws Exception { 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 

    httpclient.getCredentialsProvider().setCredentials(
      new AuthScope("localhost", 443), 
      new UsernamePasswordCredentials("username", "password")); 

    HttpGet httpget = new HttpGet("https://localhost/protected"); 

    System.out.println("executing request" + httpget.getRequestLine()); 
    HttpResponse response = httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 

    System.out.println("----------------------------------------"); 
    System.out.println(response.getStatusLine()); 
    if (entity != null) { 
     System.out.println("Response content length: " + entity.getContentLength()); 
    } 
    if (entity != null) { 
     entity.consumeContent(); 
    } 

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure 
    // immediate deallocation of all system resources 
    httpclient.getConnectionManager().shutdown();   
} 

檢查網站more examples