import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.protocol.HTTP;
public class WebServiceReqeustDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebServiceReqeustDemo demo = new WebServiceReqeustDemo();
demo.service();
}
public String service() {
String result = "";
try {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sendParamTime=formatter.format(new Date());
//System.out.print(pInXmlDoc);
PostMethod postMethod = new PostMethod("http://xxxx/article?wsdl");
StringBuffer sb = new StringBuffer();
sb.append("<soapenv:Envelope xmlns:soapenv=\"xxxxx/\" xmlns:ser=\"xxx/\">");
sb.append("<soapenv:Header/>");
sb.append("<soapenv:Body>");
sb.append("<ser:getArticleByPage>");
sb.append("<loginProperty>");
sb.append("<password>easysite</password>");
sb.append("<username>admin</username>");
sb.append("</loginProperty>");
sb.append("<columnId>31846</columnId>");
sb.append("<pageNo>1</pageNo>");
sb.append("<pageSize>3</pageSize>");
sb.append("<queryContent></queryContent>");
sb.append("<parameter></parameter>");
sb.append("</ser:getArticleByPage>");
sb.append("</soapenv:Body>");
sb.append("</soapenv:Envelope>");
postMethod.setRequestEntity(new StringRequestEntity(sb.toString(),
"application/xop+xml", HTTP.DEFAULT_CONTENT_CHARSET));
// 最后生成一个HttpClient对象,并发出postMethod请求
HttpClient httpClient = new HttpClient();
postMethod.addRequestHeader("SOAPAction", "");
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(1000*20);
int statusCode = httpClient.executeMethod(postMethod);
//String result = "";
//log.info("调中台接口返回码statusCode:"+statusCode);
if (statusCode == 200) {
System.out.println("调用成功!");
String soapResponseData = postMethod.getResponseBodyAsString();
String begin = "<soap:Envelope xmlns:soap=\"xxxx/\">";
String end = "</soap:Envelope>";
result = soapResponseData.substring(
soapResponseData.indexOf(begin) ,
soapResponseData.indexOf(end)+ end.length());
result = StringEscapeUtils.unescapeXml(result);
//String receiveParamTime=formatter.format(new Date());
String openId="";
System.out.println("返回值为:" + result);
} else {
}
//return "";
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return "";
return result;
}
}