2011-12-16 9 views
4

これは私の最初の質問ですので、私に同行してください。HttpURLConnectionからHttpClientに切り替える方法

私は、HttpURLConnectionを介してサーバからXML forのデータを取得するSwingアプリケーションを持っています。今は、サーバーとの一定の要求応答接続を作成して、アプリケーションの更新があるかどうかを確認しようとしています(チェックは定期的に行われる必要があります。

私は、ライブ接続を維持するためにHttpURLConnectionの代わりにApache HttpClientを使用する方がよいと読んだが、現在のコードからHttpClientを使用しているものへの良い例は見つかりません。具体的には、HttpURLConnection.setRequestProperty()とHttpURLConnection.getOutputStream()の代わりに何を使用するのですか?あなたはHTTPClientの4を使用している場合、私は、Apacheがすべての例を提供して考える

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 

EDITれる: posting XML to a server Http-Clientを使用した例を次に示します。

+0

おかげで、しかし、どのような私のXMLで、実際の要求テキストを追加する方法について? XMLを文字列としてpostMethodにStringEntityを追加しようとしましたが、今は "HTTP/1.1 400 Bad request"しか取得していません。 – Rolandas

+0

@Rolandas、私はURLへのリンクで答えを編集しました。チェックアウトしてください。 – Santosh

関連する問題