2013-05-30 47 views
0

我正在嘗試在Java中複製以下C#代碼。這段代碼是一個輔助類,它發送一個包含xml的請求,並讀取響應。使用Apache HttpComponents發送xml

internal static String Send(String url, String body) 
    { 
     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 
     try 
     { 
      // create the new httpwebrequest with the uri 
      request.ContentLength = 0; 
      // set the method to POST 
      request.Method = "POST"; 

      if (!String.IsNullOrEmpty(body)) 
      { 
       request.ContentType = "application/xml; charset=utf-8"; 
       byte[] postData = Encoding.Default.GetBytes(body); 
       request.ContentLength = postData.Length; 
       using (Stream s = request.GetRequestStream()) 
       { 
        s.Write(postData, 0, postData.Length); 
       } 

      } 

      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
      { 
       String responseString = new StreamReader(response.GetResponseStream()).ReadToEnd(); 
       if (response.StatusCode != HttpStatusCode.OK) 
       { 
        throw new ResponseException(((int)response.StatusCode), 
         response.StatusCode.ToString(), request.RequestUri.ToString(), 
         responseString); 
       } 
       return responseString; 
      } 
     } 
     catch (WebException e) 
     { 
      using (WebResponse response = e.Response) 
      { 
       HttpWebResponse httpResponse = response as HttpWebResponse; 
       if (httpResponse != null) 
       { 
        using (Stream data = response.GetResponseStream()) 
        { 
         data.Position = 0; 
         throw new ResponseException(((int)httpResponse.StatusCode), 
           httpResponse.StatusCode.ToString(), request.RequestUri.ToString(), 
           new StreamReader(data).ReadToEnd() 
          ); 
        } 
       } 
       else 
       { 
        throw; 
       } 

閱讀其他線程後,我確定Apache HttpComponents庫是我最好的選擇,以獲得相同的功能。在這裏閱讀文檔和下面的例子後:

http://hc.apache.org/httpcomponents-client-ga/quickstart.html

我無法弄清楚如何身體發送字符串爲XML。當我嘗試爲請求設置實體時,它要求我聲明BasicNameValuePair,並且我不明白這是什麼,或者我將如何格式化正文字符串以符合此規範。 以下是我目前所做的。

protected static String Send(String url, String body) 
{ 
    HttpPost request = new HttpPost(url); 

    try 
    { 
     request.setHeader("ContentType", "application/xml; charset=utf=8"); 

     // Encode the body if needed 
     request.setEntity(new UrlEncodedFormEntity()); 

     //get the response 

     // if the response code is not valid throw a ResponseException 

     // else return the response string. 

    } finally { 
     request.releaseConnection(); 
    } 
    return null; 
} 

編輯:或者我應該使用StringEntity並執行以下操作

protected static String SendToJetstream(String url, String body) 
{ 
    HttpPost request = new HttpPost(url); 

    try 
    { 
     StringEntity myEntity = new StringEntity(body, 
       ContentType.create("application/xml", "UTF-8")); 


     // Encode the body if needed 
     request.setEntity(myEntity); 

     //get the response 

     // if the response code is not valid throw a ResponseException 

     // else return the response string. 

    } finally { 
     request.releaseConnection(); 
    } 
    return null; 
} 

回答

相關問題