2013-05-30 1 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; 
} 

답변