안드로이드에서 HTTP POST(SOAP)을 통해 XML 전송하기

조회수 3417회

안드로이드를 통해서 웹서비스를 호출하고 싶습니다. HTTP를 통해서 특정 URL로 XML을 POST해야합니다. POST요청하는 부분까지는 올렸는데, XML 데이터를 어떻게 포함시키고 추가하는지를 모르겠네요.

public void postData() {
         // HttpClient와 Post Header 생성
         HttpClient httpclient = new DefaultHttpClient();  
         HttpPost httppost = new HttpPost("http://10.10.4.35:53011/");

         try {  
             // 데이터 추가
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
             nameValuePairs.add(new BasicNameValuePair("Content-Type", "application/soap+xml"));               
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
                 // 어디에 어떻게 XML 데이터를 추가해야되죠?


             // HTTP Post 요청 실행
             HttpResponse response = httpclient.execute(httppost);  

         } catch (ClientProtocolException e) {  
             // TODO Auto-generated catch block  
         } catch (IOException e) {  
             // TODO Auto-generated catch block  
         }  
     }

제가 원하는 POST 완료 메시지입니다.

POST /a8103e90-f1e3-11dd-bfdb-8b1fcff1a110 HTTP/1.1
Host: 10.10.4.35:53011
Content-Type: application/soap+xml
Content-Length: 602

<?xml version='1.0' encoding='UTF-8' ?>
<s12:Envelope xmlns:s12="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing">
  <s12:Header>
    <wsa:MessageID>urn:uuid:fc061d40-3d63-11df-bfba-62764ccc0e48</wsa:MessageID>
    <wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/transfer/Get</wsa:Action>
    <wsa:To>urn:uuid:a8103e90-f1e3-11dd-bfdb-8b1fcff1a110</wsa:To>
    <wsa:ReplyTo>
      <wsa:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
    </wsa:ReplyTo>
  </s12:Header>
  <s12:Body />
</s12:Envelope>

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기
    1. 우선, 런타임시 이 템플릿에서 알맞은 요청을 하기 위해 당신은 SOAP에 대한 String템플릿을 생성할 수 있고 유저가 제공하는 값을 대체할 수 있습니다.
    2. string을 StringEntity로 감싸고 content type을 text/xml로 변경합니다.
    3. entity를 SOAP 요청인자로 설정합니다. 아래와 같습니다 :
    HttpPost httppost = new HttpPost(SERVICE_EPR);          
    StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
    
    se.setContentType("text/xml");  
    httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
    httppost.setEntity(se);  
    
    HttpClient httpclient = new DefaultHttpClient();
    BasicHttpResponse httpResponse = 
        (BasicHttpResponse) httpclient.execute(httppost);
    
    response.put("HTTPStatus",httpResponse.getStatusLine().toString());
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)