发送http post请求soap服务

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

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

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


1.请求soap1.1

   1.1.java文件
    
package soap;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class TestSoap1_1 {

	public static void main(String[] args) throws Exception {
		String urlString = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
		String xmlFile = "soap1.1.xml";// 要发送的soap格式文件
		 String soapActionString = "http://WebXml.com.cn/getWeatherbyCityName";//Soap 1.1中使用
		URL url = new URL(urlString);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		File fileToSend = new File(xmlFile);
		byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组
		new FileInputStream(xmlFile).read(buf);
//		httpConn.setRequestProperty("Content-Length",
//				String.valueOf(buf.length));//这句话可以不用写,即使是随便写
		//根据我的测试,过去的请求头中的Content-Length长度也是正确的,也就是说它会自动进行计算
		httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
		 httpConn.setRequestProperty("soapActionString",soapActionString);//Soap
		httpConn.setRequestMethod("POST");
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		OutputStream out = httpConn.getOutputStream();
		out.write(buf);
		out.close();
		InputStreamReader is = new InputStreamReader(httpConn.getInputStream(),
				"utf-8");
		BufferedReader in = new BufferedReader(is);
		String inputLine;
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream("result.xml")));// 将结果存放的位置
		while ((inputLine = in.readLine()) != null) {
			System.out.println(inputLine);
			bw.write(inputLine);
			bw.newLine();
		}
		bw.close();
		in.close();
		httpConn.disconnect();
	}
}
1.2.soap1.1.xml
<?xml version="1.0" encoding="utf-8"?>
<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/">
  <soap:Body>
    <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
      <theCityName>广州</theCityName>
    </getWeatherbyCityName>
  </soap:Body>
</soap:Envelope>

2.请求soap1.2

在这里说明一下,访问soap1.2似乎很宽松,即使头部的一些信息不符合文档要求也能正常请求到数据
  2.1.java文件
  
package soap;

import java.io.*;
import java.net.*;
import javax.xml.soap.*;

public class TestSopa1_2 {
	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String urlString = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
		String xmlFile = "soap1.2.xml";// 要发送的soap格式文件
		URL url = new URL(urlString);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		File fileToSend = new File(xmlFile);
		byte[] buf = new byte[(int) fileToSend.length()];// 用于存放文件数据的数组
		new FileInputStream(xmlFile).read(buf);
//		httpConn.setRequestProperty("Content-Length",
//				String.valueOf(buf.length));//这句话可以不用写,即使是随便写
		//根据我的测试,过去的请求头中的Content-Length长度也是正确的,也就是说它会自动进行计算
		httpConn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
		httpConn.setRequestMethod("POST");
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		OutputStream out = httpConn.getOutputStream();
		out.write(buf);
		out.close();
		InputStreamReader is = new InputStreamReader(httpConn.getInputStream(),
				"utf-8");
		BufferedReader in = new BufferedReader(is);
		String inputLine;
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream("result.xml")));// 将结果存放的位置
		while ((inputLine = in.readLine()) != null) {
			System.out.println(inputLine);
			bw.write(inputLine);
			bw.newLine();
		}
		bw.close();
		in.close();
		httpConn.disconnect();
	}
}
2.2.soap1.2.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
  <soap12:Body>
    <getWeatherbyCityName xmlns="http://WebXml.com.cn/">
      <theCityName>广州</theCityName>
    </getWeatherbyCityName>
  </soap12:Body>
</soap12:Envelope>

3.建议

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



4.设置头信息

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



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
通过Post方式发送SOAP消息,可以使用以下步骤: 1. 创建SOAP请求消息体:根据SOAP协议的要求,组装SOAP消息的结构,包括SOAP Envelope、Header和Body等部分。 2. 将SOAP请求消息体转换为字符串:可以使用字符串拼接的方式,将SOAP请求消息体的各个部分拼接在一起,形成一个完整的SOAP消息字符串。 3. 创建HTTP请求:使用HTTP客户端发送POST请求,向目标服务发送SOAP消息字符串。可以使用编程语言中的HTTP库来实现,比如Java中的HttpURLConnection或HttpClient。 4. 设置HTTP请求头信息:在HTTP请求中设置合适的Content-Type和Content-Length等HTTP头信息,以告知服务端接收的数据类型和数据长度。 5. 发送SOAP消息:将SOAP消息字符串作为请求体内容发送到目标服务端,完成发送过程。 6. 处理服务端响应:接收服务端返回的响应消息,可以使用编程语言中的HTTP库读取Response对象,并解析其内容,获取服务端返回的SOAP响应消息。 7. 解析SOAP响应消息:提取SOAP响应消息的各个部分,可以使用字符串截取等方式来获取SOAP Envelope、Header和Body等部分的内容,并进行相应的处理。 需要注意的是,SOAP消息的格式和内容需要按照相应的SOAP协议规范进行组装和解析,以确保发送和接收的消息能够正确被服务端处理和解析。另外,在发送SOAP请求时,还需要确保目标服务端的地址和端口号等信息正确,并且服务端能够正确处理和解析接收到的SOAP消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值