- 1 import java.io.BufferedReader;
- 2 import java.io.DataOutputStream;
- 3 import java.io.InputStream;
- 4 import java.io.InputStreamReader;
- 5 import java.net.HttpURLConnection;
- 6 import java.net.URL;
- 7
- 8 import org.apache.commons.httpclient.HttpClient;
- 9 import org.apache.commons.httpclient.methods.PostMethod;
- 10 import org.apache.commons.httpclient.methods.RequestEntity;
- 11 import org.apache.commons.httpclient.methods.StringRequestEntity;
- 12 import org.apache.commons.io.IOUtils;
- 13
- 14 public class TestHelloWrold {
- 15 public static voidmain(String[] args)throws Exception {
- 16String wsdl = "http://localhost:9000/HelloWorld?wsdl";
- 17 inttimeout = 10000;
- 18StringBuffer sb =newStringBuffer("");
- 19sb.append("");
- 20sb.append("21+ "xmlns:api='http://demo.ls.com/' "22+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "23+ "xmlns:xsd='http://www.w3.org/2001/XMLSchema' "24+ "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>");
- 25sb.append("");
- 26sb.append("");
- 27sb.append("ls");
- 28sb.append("");
- 29sb.append("");
- 30sb.append("");
- 31
- 32
- 33
- 34 // HttpClient发送SOAP请求
- 35System.out.println("HttpClient 发送SOAP请求");
- 36HttpClient client =new HttpClient();
- 37PostMethod postMethod =new PostMethod(wsdl);
- 38 // 设置连接超时
- 39 client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
- 40 // 设置读取时间超时
- 41 client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
- 42 // 然后把Soap请求数据添加到PostMethod中
- 43RequestEntity requestEntity =newStringRequestEntity(sb.toString(), "text/xml", "UTF-8");
- 44 //设置请求头部,否则可能会报 "no SOAPAction header" 的错误
- 45postMethod.setRequestHeader("SOAPAction","");
- 46 // 设置请求体
- 47 postMethod.setRequestEntity(requestEntity);
- 48 intstatus = client.executeMethod(postMethod);
- 49 // 打印请求状态码
- 50System.out.println("status:" + status);
- 51 // 获取响应体输入流
- 52InputStream is = postMethod.getResponseBodyAsStream();
- 53 // 获取请求结果字符串
- 54String result = IOUtils.toString(is);
- 55System.out.println("result: " + result);
- 56
- 57
- 58
- 59 // HttpURLConnection 发送SOAP请求
- 60System.out.println("HttpURLConnection 发送SOAP请求");
- 61URL url =new URL(wsdl);
- 62HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- 63
- 64conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
- 65conn.setRequestMethod("POST");
- 66conn.setUseCaches(false);
- 67conn.setDoInput(true);
- 68conn.setDoOutput(true);
- 69 conn.setConnectTimeout(timeout);
- 70 conn.setReadTimeout(timeout);
- 71
- 72DataOutputStream dos =new DataOutputStream(conn.getOutputStream());
- 73dos.write(sb.toString().getBytes("utf-8"));
- 74 dos.flush();
- 75
- 76
- 77BufferedReader reader =newBufferedReader(newInputStreamReader(conn.getInputStream(), "utf-8"));
- 78String line =null;
- 79StringBuffer strBuf =new StringBuffer();
- 80 while((line = reader.readLine()) !=null) {
- 81 strBuf.append(line);
- 82 }
- 83 dos.close();
- 84 reader.close();
- 85
- 86 System.out.println(strBuf.toString());
- 87 }
- 88
- 89}
来源: http://www.cnblogs.com/redjh/p/6841665.html