import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import com.css.apps.base.dict.service.DictMan;
public class sendHttp {
private static Log log = LogFactory.getLog(sendHttp .class);
public static Map<String, List<String>> a = new HashMap<String, List<String>>();
public static List<String> nodeNameList = new ArrayList<>();
public static Map sendHttpTest(String loginName,String flag,String number) throws IOException {
Map c = new HashMap();
//第一步:创建服务地址,不是WSDL地址
String urlstr = DictMan.getDictType("yh_interface_firstpage", "6").getName();
URL url = new URL(urlstr);
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod("POST");
//3.2设置数据格式:content-type
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
//判断是否是全区会议通知还是我的会议通知
String soapXML = null;
soapXML = getXMLRegionMeetingNotice(loginName,number);
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
//第五步:接收服务端响应,打印(xml格式数据)
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服务端响应成功
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringBuilder sb = null;
try {
is = connection.getInputStream();
isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
c = convertXmlToMap(sb.toString());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
br.close();
isr.close();
is.close();
}
}
os.close();
return c;
}
/**
* xml报文转换为map
* @author lpx
*/
public static Map<String, List<String>> convertXmlToMap(String xmldata){
Map<String, List<String>> map=new HashMap<String, List<String>>();
Document doc=null;
try {
//将字符串转换为doc
doc=DocumentHelper.parseText(xmldata);
//获取根节点
Element root=doc.getRootElement();
map=getNodes(root);
nodeNameList = new ArrayList<>();
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 从指定节点Element node开始,递归遍历其所有子节点
*/
@SuppressWarnings("unchecked")
public static Map<String, List<String>> getNodes(Element node) {
// 当前节点的名称、文本内容和属性
String nodeName = node.getName();// 当前节点名称
String nodeText = node.getTextTrim();// 当前节点内容
//节点名为messageText加入map中去
if(!node.getTextTrim().isEmpty() && "messageText".equals(nodeName)){
nodeNameList.add(nodeText);
a.put("messageText",nodeNameList);
}
// 递归遍历当前节点所有的子节点
final List<Element> listElement = node.elements();// 所有一级子节点的list
for (final Element e : listElement) {// 遍历所有一级子节点
getNodes(e);// 递归
}
return a;
}
public static String getXMLRegionMeetingNotice(String loginName,String number){
String _xmlInfo = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">"
+ "<soap:Header/>"
+ "<soap:Body>"
+ "<tem:GetQqHytz>"
+ "<tem:xmlDoc>"
+ "<Root>"
+ "<Document>"
+ "<LoginName>"
+ loginName
+ "</LoginName>"
+ "<Number>"
+ number
+ "</Number>"
+ "</Document>"
+ "</Root>"
+ "</tem:xmlDoc>"
+ "</tem:GetQqHytz>"
+ "</soap:Body>"
+ "</soap:Envelope>" ;
return _xmlInfo;
}
}
感谢某位博主写的博文,这里面仅仅起到记载作用,但是那位博文因为浏览器清缓存,地址找不到了。。。。。┓(;´_`)┏