Httpclient 发送XML

package com.actions;

public interface IClient {
 
 public String XML_HEADER = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";

 

}

package com.actions;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
public class XMLClient {
 
 private DefaultHttpClient client;

 public String getXMLString() {
   StringBuffer sb = new StringBuffer();
    sb.append(IClient.XML_HEADER);
    sb.append("<AastraIPPhoneInputScreen type=\"string\">");
    sb.append("<Title>Hello world!</Title>");
    sb.append("<Prompt>Enter value</Prompt>");
    sb.append("<URL>http://localhost/xmlserver/test.do</URL>");
    sb.append("<Parameter>value</Parameter>");
    sb.append("<Default></Default>");
    sb.append("</AastraIPPhoneInputScreen>");
    return sb.toString();
  }
 public Integer sendXMLDataByPost(String url, String xmlData)throws ClientProtocolException, IOException {
       Integer statusCode = -1;
       if (client == null) {
    client = new DefaultHttpClient();
   }
      HttpPost post = new HttpPost(url);
      StringEntity entity = new StringEntity(xmlData);
      post.setEntity(entity);
      post.setHeader("Content-Type", "text/xml;charset=ISO-8859-1");
      HttpResponse response = client.execute(post);
      statusCode = response.getStatusLine().getStatusCode();
      return statusCode;
 }

 public void testAA() throws ClientProtocolException, IOException {
     XMLClient client = new XMLClient();
     Integer statusCode = client.sendXMLDataByPost("http://localhost:8080/testhttp/test", client.getXMLString());
     if(statusCode==200){
      System.out.println("Request Success,Response Success!!!");
     }else{
      System.out.println("Response Code :"+statusCode);
     }


 
 
 }
}

你可能感兴趣的:(xml)