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);
}
}