如何通过http client post一个xml片段

 我来答
育知同创教育
2016-04-25 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
参考一下代码
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);
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式