Android,通过HTTP POST(SOAP)发送XML

Jus*_*axi 46 xml post android soap http

我想通过Android调用web服务.我需要通过HTTP将一些XML发布到URL.我发现这是剪辑发送POST,但我不知道如何包含/添加XML数据本身.

public void postData() {
         // Create a new HttpClient and Post Header  
         HttpClient httpclient = new DefaultHttpClient();  
         HttpPost httppost = new HttpPost("http://10.10.4.35:53011/");

         try {  
             // Add your data  
             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);  
             nameValuePairs.add(new BasicNameValuePair("Content-Type", "application/soap+xml"));               
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
                 // Where/how to add the XML data?


             // Execute HTTP Post Request  
             HttpResponse response = httpclient.execute(httppost);  

         } catch (ClientProtocolException e) {  
             // TODO Auto-generated catch block  
         } catch (IOException e) {  
             // TODO Auto-generated catch block  
         }  
     }
Run Code Online (Sandbox Code Playgroud)

这是我需要模仿的完整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>
Run Code Online (Sandbox Code Playgroud)

Sam*_*muh 47

  1. 首先,您可以为此SOAP请求创建String模板,并在此模板中在运行时替换用户提供的值以创建有效请求.
  2. 将此字符串包装在StringEntity中,并将其内容类型设置为text/xml
  3. 在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());
Run Code Online (Sandbox Code Playgroud)

  • 当然,我只是粘贴了与我正在制作的相关的部分......很高兴知道它有所帮助.干杯! (2认同)
  • 喜.我应该把SOAPRequestXML ="POST/a8103e .... <s12:Body /> </ s12:Envelope>"或"<?xml version ='1.0'....... <s12:Body /> </S12:信封>"? (2认同)
  • @Samuh你能否清楚一下代码的最后一行`response.put(...);`是什么以及对象`response`是什么类型的? (2认同)

Hel*_*miB 7

这里是发送肥皂消息的替代方案.

public String setSoapMsg(String targetURL, String urlParameters){

        URL url;
        HttpURLConnection connection = null;  
        try {
          //Create connection
          url = new URL(targetURL);

         // for not trusted site (https)
         // _FakeX509TrustManager.allowAllSSL();
         // System.setProperty("javax.net.debug","all");

          connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("POST");


          connection.setRequestProperty("SOAPAction", "**** SOAP ACTION VALUE HERE ****");

          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);


          //Send request
          DataOutputStream wr = new DataOutputStream (
                       connection.getOutputStream ());
          wr.writeBytes (urlParameters);
          wr.flush ();
          wr.close ();

          //Get Response    
          InputStream is ;
          Log.i("response", "code="+connection.getResponseCode());
          if(connection.getResponseCode()<=400){
              is=connection.getInputStream();
          }else{
              /* error from server */
              is = connection.getErrorStream();
        } 
         // is= connection.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
          String line;
          StringBuffer response = new StringBuffer(); 
          while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
          }
          rd.close();
          Log.i("response", ""+response.toString());
          return response.toString();

        } catch (Exception e) {

         Log.e("error https", "", e);
          return null;

        } finally {

          if(connection != null) {
            connection.disconnect(); 
          }
        }
      }
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.如果有人想知道allowAllSSL()方法,谷歌吧:).


Max*_*ner 5

所以如果你使用:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Run Code Online (Sandbox Code Playgroud)

它仍然是休息,但如果您使用:

StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
httppost.setEntity(se);  
Run Code Online (Sandbox Code Playgroud)

这是肥皂???