接口 类 httputil

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoleizhanghahaha/article/details/53415406

package com.ceair.weixin.util;


import java.io.ByteArrayInputStream;
import java.io.InputStream;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


public class HttpUtil {
private static Logger logger = LogManager.getLogger(HttpUtil.class);


public static String httpPost(String url, String request) {
String response = "";
PostMethod postMethod = new PostMethod(url);
try {
byte[] b = request.getBytes("utf-8");
InputStream is = new ByteArrayInputStream(b, 0, b.length);
RequestEntity re = new InputStreamRequestEntity(is, b.length,
"text/xml;charset=UTF-8");
postMethod.setRequestEntity(re);
// 最后生成一个HttpClient对象,并发出postMethod请求
HttpClient httpClient = new HttpClient();
// 执行这个web服务,并返回这个的状态
int statusCode = httpClient.executeMethod(postMethod);
if (statusCode == 200) {
// 得到web服务返回的数据
response = postMethod.getResponseBodyAsString();
// 释放连接
postMethod.releaseConnection();
} else {
System.out.println("http调用失败:code-" + statusCode);
}
} catch (Exception e) {
logger.error("http调用失败:", e);
}
return response;
}

}

-------------------------------------------------------

使用方法

String request = getTasksNodeListRequest(topProcInstID);
// 发送请求
String response = HttpUtil.httpPost(PushUrl.GET_TASKS_NODE,
request);

---------------------------------

请求

private String getTasksNodeListRequest(String topProcInstID)
throws Exception {
String detailStr = "";
detailStr += "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:tem='http://tempuri.org/'>";
detailStr += "<soapenv:Header/>";
detailStr += "<soapenv:Body>";
detailStr += "<tem:GetFlowDoNodes>";
detailStr += "<tem:topProcInstID>" + topProcInstID + "</tem:topProcInstID>";
detailStr += "</tem:GetFlowDoNodes>";
detailStr += "</soapenv:Body>";
detailStr += "</soapenv:Envelope>";
return detailStr;
}

猜你喜欢

转载自blog.csdn.net/xiaoleizhanghahaha/article/details/53415406