分享

使用HttpClient通过POST方式发送XML

 昵称20874412 2015-05-03

以下是代码的示例,目前还未真正测试过服务器端到底能不能接受到这次请求数据,只是简单观察了下 TCP/IP Monitor, 后期会投入一定的时间继续深入这套API组件.

IClient.java

package com.apt.client;

/**
* Constant Interface to define the normal Constant in this application

* @author Lv Pin

*/

public interface IClient {

     /**
      * The XML Header of every XML string
     */
      public String XML_HEADER = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
}

XMLClient.java

package com.apt.client;

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;

/**
* HTTP Client, to send data of XML type to Server. This is a demonstration of
* how to use HTTP Client API

* @author Lv Pin

*/
public class XMLClient {

  /**
    * HTTP Client Object,used HttpClient Class before(version 3.x),but now the
    * HttpClient is an interface
    */
   private DefaultHttpClient client;

  /**
     * Get XML String
     * 
     * @return XML-Formed string
   */
public String getXMLString() {
// A StringBuffer Object
  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 to String Formed
  return sb.toString();
}

/**
  * Send a XML-Formed string to HTTP Server by post method
  * 
  * @param url
  *            the request URL string
  * @param xmlData
  *            XML-Formed string ,will not check whether this string is
  *            XML-Formed or not
  * @return the HTTP response status code ,like 200 represents OK,404 not
  *         found
  * @throws IOException
  * @throws ClientProtocolException
  */
public Integer sendXMLDataByPost(String url, String xmlData)
   throws ClientProtocolException, IOException {
  Integer statusCode = -1;
  if (client == null) {
   // Create HttpClient Object
   client = new DefaultHttpClient();
  }
// Send data by post method in HTTP protocol,use HttpPost instead of
  // PostMethod which was occurred in former version
  HttpPost post = new HttpPost(url);
// Construct a string entity
  StringEntity entity = new StringEntity(xmlData);
  // Set XML entity
  post.setEntity(entity);
  // Set content type of request header
  post.setHeader("Content-Type", "text/xml;charset=ISO-8859-1");
// Execute request and get the response
  HttpResponse response = client.execute(post);
  // Response Header - StatusLine - status code
  statusCode = response.getStatusLine().getStatusCode();
  return statusCode;
}

/**
  * Main method 
  * @param args
  * @throws IOException 
  * @throws ClientProtocolException 
  */
public static void main(String[] args) throws ClientProtocolException, IOException {
  XMLClient client = new XMLClient();
  Integer statusCode = client.sendXMLDataByPost("http://localhost:8081", client.getXMLString());
  if(statusCode==200){
   System.out.println("Request Success,Response Success!!!");
  }else{
   System.out.println("Response Code :"+statusCode);
  }
}
}


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多