import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Scanner;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;
public class HttpUtil {
public static String sendSoapPost(String url, String xml,
String contentType, String soapAction) {
String urlString = url;
HttpURLConnection httpConn = null;
OutputStream out = null;
String returnXml = "";
try {
httpConn = (HttpURLConnection) new URL(urlString).openConnection();
httpConn.setRequestProperty("Content-Type", contentType);
if (null != soapAction) {
httpConn.setRequestProperty("SOAPAction", soapAction);
}
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
httpConn.connect();
out = httpConn.getOutputStream(); // 获取输出流对象
httpConn.getOutputStream().write(xml.getBytes("UTF-8")); // 将要提交服务器的SOAP请求字符流写入输出流
out.flush();
out.close();
int code = httpConn.getResponseCode(); // 用来获取服务器响应状态
String tempString = null;
StringBuffer sb1 = new StringBuffer();
if (code == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getInputStream(),
"UTF-8"));
while ((tempString = reader.readLine()) != null) {
sb1.append(tempString);
}
if (null != reader) {
reader.close();
}
} else {
BufferedReader reader = new BufferedReader(
new InputStreamReader(httpConn.getErrorStream(),
"UTF-8"));
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
sb1.append(tempString);
}
if (null != reader) {
reader.close();
}
}
// 响应报文
returnXml = sb1.toString();
} catch (Exception e) {
e.printStackTrace();
}
return returnXml;
}
@Test
public void Test() throws DocumentException {
String url = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx";
StringBuilder sb = new StringBuilder("");
System.out.println("输入手机号:");
@SuppressWarnings("resource")
Scanner scan=new Scanner(System.in);
String phone=scan.next();
sb.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">")
.append(" <soap:Body>" )
.append(" <getMobileCodeInfo xmlns=\"http://WebXml.com.cn/\">" )
.append(" <mobileCode>"
+ phone
+ "</mobileCode>" )
.append(" <userID></userID>" )
.append(" </getMobileCodeInfo>")
.append(" </soap:Body>" )
.append("</soap:Envelope>");
String dataXml = sb.toString();
//System.out.println(dataXml);
String contentType = "text/xml; charset=utf-8";
String soapAction = "http://WebXml.com.cn/getSupportProvince";
soapAction="http://WebXml.com.cn/getMobileCodeInfo";
// String soapAction =
// "\"document/http://pengjunnlee.com/CustomUI:GetWeatherById\"";
String resultXml = HttpUtil.sendSoapPost(url, dataXml, contentType,
soapAction);
//使用dom4j解析String Xml
Document document = DocumentHelper.parseText(resultXml);
//Element root = document.getRootElement();
@SuppressWarnings("unchecked")
List<Element> list = document.selectNodes("/soap:Envelope/*[name()='soap:Body']/*[name()='getMobileCodeInfoResponse']/*[name()='getMobileCodeInfoResult']");
for (Element element : list) {
String str=element.getText();
System.out.println(str);
}
}
@Test
public void test2() throws DocumentException {
for (int i = 0; i > -1; i++) {
Test();
}
}
}
使用HttpURLConnection调用soap webservice(来源网络)
最新推荐文章于 2024-04-30 10:50:19 发布