2011-12-16 62 views
4

這是我的第一個問題,所以請,請耐心等待。如何從HttpURLConnection切換到HttpClient

我有一個Swing應用程序,它通過HttpURLConnection從服務器獲取XML數據。現在我試圖創建一個與服務器連續的請求 - 響應連接,以檢查應用程序是否有任何更新(因爲檢查必須定期和經常進行(每秒鐘左右))。

在一些問題的評論中,我讀到使用Apache HttpClient而不是HttpURLConnection來維持活動連接會更好,但是我找不到任何好的示例如何從我的當前代碼轉到使用HttpClient的代碼。具體來說,使用什麼來代替HttpURLConnection.setRequestProperty()和HttpURLConnection.getOutputStream()?

Document request = new Document(xmlElement); 
Document response = new Document(); 

String server = getServerURL(datasetName); 
try { 
    URL url = new URL(server); 
    try { 
    HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
    connection.setRequestProperty("Content-Type","application/xml; charset=ISO-8859-1"); 
    connection.setDoOutput(true); 
    connection.setDoInput(true); 
    connection.setRequestMethod("POST"); 
    OutputStream output = connection.getOutputStream(); 

    XMLOutputter serializer = new XMLOutputter(); 
    serializer.output(request, output); 

    output.flush(); 
    output.close(); 

    InputStream input = connection.getInputStream(); 
    String tempString = ErrOut.printToString(input); 

    SAXBuilder parser = new SAXBuilder(); 
    try { 
     response = parser.build(new StringReader(tempString)); 
    } 
    catch (JDOMException ex) { ... } 
    input.close(); 
    connection.disconnect(); 
    } 
    catch (IOException ex) { ... } 
} 
catch (MalformedURLException ex) { ... } 

回答

4

謝謝Santosh和Raveesh Sharma的回答。 我結束了使用StringEntity,這就是我現在有:

Document request = new Document(xmlElement); 
Document response = new Document(); 

XMLOutputter xmlOutputter = new XMLOutputter(); 
String xml = xmlOutputter.outputString(request); 

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost(getServerURL(datasetName)); 
post.setHeader("Content-type", "application/xml; charset=ISO-8859-1"); 

try 
{ 
    StringEntity se = new StringEntity(xml); 
    se.setContentType("text/xml"); 
    post.setEntity(se); 
} 
catch (UnsupportedEncodingException e) { ... } 

try 
{ 
    HttpResponse httpResponse = client.execute(post); 

    BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent())); 
    String line = ""; 
    String tempString = ""; 
    while ((line = rd.readLine()) != null) 
    { 
    tempString += line; 
    } 

    SAXBuilder parser = new SAXBuilder(); 
    try 
    { 
    response = parser.build(new StringReader(tempString)); 
    } 
    catch (JDOMException ex) { ... } 
} 
catch (IOException ex) { ... } 
3

這裏的片斷,你需要(這取代了大多數的你在try塊擁有的東西),

try { 
String postURL= server; 
HttpClient client = new HttpClient(); 
PostMethod postMethod = new PostMethod(postURL);; 
client.executeMethod(postMethod); 
InputStream input = postMethod.getResponseBodyAsStream(); 
//--your subsequent code here 

編輯: 以下是posting XML to a server使用Http-Client的示例。

+0

謝謝,但和我的XML將實際要求的文字是什麼? 我試圖將StringEntity添加到postMethod中,將XML作爲字符串,但現在我只能得到「HTTP/1.1 400 Bad request」。 – Rolandas 2011-12-16 14:39:16