发送http post请求soap服务

原文链接

目录(?)[+]

     要访问的webservice服务说明文档:

                   根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数 

                     http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getWeatherbyCityName


1.请求soap1.1

   1.1.java文件
    
[html]  view plain  copy
  1. package soap;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.BufferedWriter;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.InputStreamReader;  
  9. import java.io.OutputStream;  
  10. import java.io.OutputStreamWriter;  
  11. import java.net.HttpURLConnection;  
  12. import java.net.URL;  
  13.   
  14. public class TestSoap1_1 {  
  15.   
  16.     public static void main(String[] args) throws Exception {  
  17.         String urlString = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";  
  18.         String xmlFile = "soap1.1.xml";// 要发送的soap格式文件  
  19.          String soapActionString = "http://WebXml.com.cn/getWeatherbyCityName";//Soap 1.1中使用  
  20.         URL url = new URL(urlString);  
  21.         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
  22.         File fileToSend = new File(xmlFile);  
  23.         byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组  
  24.         new FileInputStream(xmlFile).read(buf);  
  25. //      httpConn.setRequestProperty("Content-Length",  
  26. //              String.valueOf(buf.length));//这句话可以不用写,即使是随便写  
  27.         //根据我的测试,过去的请求头中的Content-Length长度也是正确的,也就是说它会自动进行计算  
  28.         httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");  
  29.          httpConn.setRequestProperty("soapActionString",soapActionString);//Soap  
  30.         httpConn.setRequestMethod("POST");  
  31.         httpConn.setDoOutput(true);  
  32.         httpConn.setDoInput(true);  
  33.         OutputStream out = httpConn.getOutputStream();  
  34.         out.write(buf);  
  35.         out.close();  
  36.         InputStreamReader is = new InputStreamReader(httpConn.getInputStream(),  
  37.                 "utf-8");  
  38.         BufferedReader in = new BufferedReader(is);  
  39.         String inputLine;  
  40.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(  
  41.                 new FileOutputStream("result.xml")));// 将结果存放的位置  
  42.         while ((inputLine = in.readLine()) != null) {  
  43.             System.out.println(inputLine);  
  44.             bw.write(inputLine);  
  45.             bw.newLine();  
  46.         }  
  47.         bw.close();  
  48.         in.close();  
  49.         httpConn.disconnect();  
  50.     }  
  51. }  
1.2.soap1.1.xml
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <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/">  
  3.   <soap:Body>  
  4.     <getWeatherbyCityName xmlns="http://WebXml.com.cn/">  
  5.       <theCityName>广州</theCityName>  
  6.     </getWeatherbyCityName>  
  7.   </soap:Body>  
  8. </soap:Envelope>  

2.请求soap1.2

在这里说明一下,访问soap1.2似乎很宽松,即使头部的一些信息不符合文档要求也能正常请求到数据
  2.1.java文件
  
[java]  view plain  copy
  1. package soap;  
  2.   
  3. import java.io.*;  
  4. import java.net.*;  
  5. import javax.xml.soap.*;  
  6.   
  7. public class TestSopa1_2 {  
  8.     /** 
  9.      * @param args 
  10.      * @throws Exception 
  11.      */  
  12.     public static void main(String[] args) throws Exception {  
  13.         String urlString = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";  
  14.         String xmlFile = "soap1.2.xml";// 要发送的soap格式文件  
  15.         URL url = new URL(urlString);  
  16.         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
  17.         File fileToSend = new File(xmlFile);  
  18.         byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组  
  19.         new FileInputStream(xmlFile).read(buf);  
  20. //      httpConn.setRequestProperty("Content-Length",  
  21. //              String.valueOf(buf.length));//这句话可以不用写,即使是随便写  
  22.         //根据我的测试,过去的请求头中的Content-Length长度也是正确的,也就是说它会自动进行计算  
  23.         httpConn.setRequestProperty("Content-Type""application/soap+xml; charset=utf-8");  
  24.         httpConn.setRequestMethod("POST");  
  25.         httpConn.setDoOutput(true);  
  26.         httpConn.setDoInput(true);  
  27.         OutputStream out = httpConn.getOutputStream();  
  28.         out.write(buf);  
  29.         out.close();  
  30.         InputStreamReader is = new InputStreamReader(httpConn.getInputStream(),  
  31.                 "utf-8");  
  32.         BufferedReader in = new BufferedReader(is);  
  33.         String inputLine;  
  34.         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(  
  35.                 new FileOutputStream("result.xml")));// 将结果存放的位置  
  36.         while ((inputLine = in.readLine()) != null) {  
  37.             System.out.println(inputLine);  
  38.             bw.write(inputLine);  
  39.             bw.newLine();  
  40.         }  
  41.         bw.close();  
  42.         in.close();  
  43.         httpConn.disconnect();  
  44.     }  
  45. }  
2.2.soap1.2.xml
[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">  
  3.   <soap12:Body>  
  4.     <getWeatherbyCityName xmlns="http://WebXml.com.cn/">  
  5.       <theCityName>广州</theCityName>  
  6.     </getWeatherbyCityName>  
  7.   </soap12:Body>  
  8. </soap12:Envelope>  

3.建议

最好是下载一个wireshark,因为这样你就能够查看你到底发送什么东西过去了
以下是我发送的信息:



4.设置头信息

如果运行不了,请查看头信息,并且可以使用httpConn.setRequestProperty设置Host,Accept等


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值