http post 发送xml数据

本文介绍了一个针对WCDMA视频服务的HTTP客户端实现方案,该客户端使用Java语言开发,通过POST方法发送XML格式的数据请求,并接收服务端响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package WCDMAVideoHttpClient;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;


public class HttpClientUtil {
	
	private HttpClient httpClient = null;
	
	private String url = null;
	
	private String xml = null;
	

	
	public HttpClientUtil(String url, String xml){
		httpClient = new HttpClient();
		this.url = url;
		this.xml = xml;
	}
	
	
	
	public String sendXMLReq(){
		
		PostMethod post = new PostMethod(url);
		//add xml
		RequestEntity requestXml = null;;
		try {
			requestXml = new StringRequestEntity(xml, "text/xml", "utf-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		post.setRequestEntity(requestXml);
		
		//execute post 
		try {
			httpClient.executeMethod(post);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//get resp info
		
		String resp = null;
		try {
			resp = post.getResponseBodyAsString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		post.releaseConnection();
		return resp;
	}
	public static void main(String[] args) {
		HttpClientUtil HttpClientUtil = new HttpClientUtil("url:这是对方的接口","xml:将发的数据以xml的形式发送到对方");
		
		//这里是通过sendXmlReq方法将请求发送出去,返回值为对付给我们的相应,具体返回的格式和联通定义
		String str = HttpClientUtil.sendXMLReq();
		//然后我们解析str就是对方给我们信息了这次请求就算结束了
		System.out.println(str);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值