栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java 发送soap请求

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 发送soap请求

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

广州

2.请求soap1.2

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

2.1.java文件

package soap;

import java.io.*;

import java.net.*;

import javax.xml.soap.*;

public class TestSopa1_2 {

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

广州

3.建议

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

以下是我发送的信息:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023241.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号