`
xuelianbobo
  • 浏览: 173657 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

Transport level information does not match with SOAP Message namespace URI

阅读更多
package com.xue.client;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import com.xue.conf.MyConfig;
import com.xue.safswsHelper.Xexception;
import com.xue.tools.text.MyTextUtil;

public class HttpClient {
	static Logger logger = Logger.getLogger(HttpClient.class);

	/**
	 * httpclient方法,可以实现webservice客户端的 即 post方法 body是xml
	 * 
	 * @param urlStr
	 *            web请求的url
	 * @param headers
	 *            http的headers
	 * @param body
	 *            post请求的body
	 * @param charSet
	 *            字符编码
	 * @return 返回http的请求响应
	 */
	public static String call(String urlStr, Map<String, String> headers, String body, String charSet) {
		HttpURLConnection httpConn = null;
		try {
			logger.info("request :" + urlStr);
			URL url = new URL(urlStr);
			httpConn = (HttpURLConnection) url.openConnection();
			// 设置conten-length
			httpConn.setRequestProperty("Content-Length", String.valueOf(body.getBytes(charSet).length));
			Set<String> keySet = headers.keySet();
			// 设置head
			for (String headerName : keySet) {
				if (headerName.equalsIgnoreCase("Method")) {
					continue;
				}
				httpConn.setRequestProperty(headerName, headers.get(headerName));
			}
			// 设置方法
			httpConn.setRequestMethod(headers.get("Method"));
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);
			logger.debug("request text:" + new String(body.getBytes(charSet), charSet));
			OutputStream out = httpConn.getOutputStream();
			out.write(body.getBytes(charSet));
			out.close();
			String result = readInputStream(httpConn.getInputStream(), charSet);
			logger.debug("response text:" + result);
			logger.info("request end");
			// 打印返回结果
			return result;
		} catch (Exception e) {
			// 发生异常的时候 有可能是发生了错误 即响应码不是200
			InputStream errorStream = httpConn.getErrorStream();
			try {
				e.printStackTrace();
				String result = readInputStream(errorStream, charSet);
				System.out.println(new String(result));
				throw new Xexception("http request error:" + result);

			} catch (IOException e1) {
				e1.printStackTrace();
				throw new Xexception("http request error:" + e1.getMessage());
			}
		} finally {
			httpConn.disconnect();
		}

	}

	/**
	 * 从流中读取字符串
	 * 
	 * @param inStream
	 *            输入流
	 * @param charSet
	 *            字符编码
	 * @return 对应的字符串
	 * @throws IOException
	 */
	private static String readInputStream(InputStream inStream, String charSet) throws IOException {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();// 网页的二进制数据
		outStream.close();
		inStream.close();
		return new String(data, charSet);
	}

	/***
	 * 测试方法
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		MyConfig.getTableNames();
		String urlStr = "http://136.64.21.133:8080/SAFSWS/INASService?wsdl";
		int port = 9020;
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Method", "POST");
		headers.put("Accept-Encoding", "gzip,deflate");
		/**
		 * 下面这行是报错的原因 因为axis框架会有两个版本的soap 具体可以这么分析 如果namespace 是
		 * "http://schemas.xmlsoap.org/soap/envelope/", 那就是 SOAP 1.1 message
		 * 如果namespace 是 "http://www.w3.org/2003/05/soap-envelope", 那就是 SOAP 1.2
		 * message. 如果是1.1必须有soapaction 如果是1.2则有可能要将soapaction包含在content-type里面
		 */
		headers.put("Content-Type", "text/xml;charset=UTF-8");
		headers.put("Content-Type", "application/soap+xml;charset=UTF-8;action=\"urn:queryBriefMsg\"");
		headers.put("SOAPAction", "");
		String body = null;
		try {
			// 该出的body 大家可以自定义
			body = MyTextUtil.getTextFromFile(new File("src/com/xue/client/httpRequestTemplate.txt"), "utf-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		call(urlStr, headers, body, "utf-8");
	}
}

 

最近在调试webservice接口的时候遇到了该问题,经过查找资料得出了解决方法。

首先我的客户端没有用任何三方框架,直接就是http+xml来实现。 具体的看代码如上:

 

分享到:
评论

相关推荐

    自定义SOAP消息详细解释

    自定义SOAP消息主要涉及到在使用ASP.NET创建的XML Web服务中,如何控制SOAP消息的格式。SOAP(简单对象访问协议)是一种轻量级的、基于XML的协议,用于在Web服务中交换结构化和类型化的信息。SOAP消息通常包含一个...

    Hive Beeline连接报错:Could not open client transport with JDBC Uri: jdbc:hive2://hcmaster:10000/default

    在使用Hive的Beeline客户端进行连接时,可能会遇到错误提示"Could not open client transport with JDBC Uri: jdbc:hive2://hcmaster:10000/default"。这个错误通常意味着Beeline无法通过JDBC URL成功建立到Hive ...

    Package with uri not found.doc

    在Eclipse插件开发中,遇到“Package with uri not found”的错误通常是由于EMF(Eclipse Modeling Framework)在加载模型时找不到对应的ECORE文件。这个问题可以从多个角度来分析和解决。 首先,我们要理解EMF的...

    Web 服务编程技巧和窍门: 手工创建的 SOAP 消息中命名空间的处理

    JAX-WS提供了丰富的API,如`javax.xml.soap`包中的`MessageFactory`、`SOAPMessage`、`SOAPPart`和`SOAPEnvelope`等类,用于构建和操作SOAP消息。 创建SOAP消息时,要处理命名空间,你需要: 1. 创建`SOAPMessage`...

    CXF打印SOAP报文,记录WebService日志

    String uri = (String) message.get(Message.REQUEST_URI); if (uri != null) { buffer.getAddress().append(uri); } InputStream is = (InputStream) message.getContent(InputStream.class); if (is != null...

    soap1.1和soap1.2区别

    第三,SOAP 1.1 和 SOAP 1.2 的命名空间(Namespace)不同。SOAP 1.1 使用 "http://schemas.xmlsoap.org/soap/envelope/" 作为命名空间,而 SOAP 1.2 使用 "http://www.w3.org/2003/05/soap-envelope"。这意味着 ...

    SOAP UI Open Source Testing with SOAP UI

    ### SOAP UI Open Source Testing知识点详解 #### 摘要与目标用户 - **摘要**:本文档主要介绍了如何使用SOAP UI进行开源测试。SOAP UI是一款功能强大的工具,用于API的功能性和回归性测试,特别是针对SOAP和REST...

    Java SOAP协议

    SOAPMessage message = messageFactory.createMessage(); // 获取SOAP信封 SOAPPart part = message.getSOAPPart(); SOAPEnvelope envelope = part.getEnvelope(); // 设置SOAP头 SOAPHeader header = ...

    Android Uri

    "Android Uri详解" Android Uri是Android系统中的一种通用资源标志符(Universal Resource Identifier, 简称"URI"),它代表要操作的数据。Android 上可用的每种资源 - 图像、视频片段等都可以用 Uri 来表示。Uri ...

    PHP-soap接口服务器

    $server = new SoapServer(null, array('uri' =&gt; 'http://example.com/namespace')); $server-&gt;setClass('MyService'); $server-&gt;handle(); ``` 在这个例子中,`MyService`类的`myOperation`方法会被SOAP客户端调用...

    Uri一个URI处理库

    - **规范化和比较URI**:Uri库支持URI的规范化,确保相同含义的URI以相同的格式表示,同时提供了比较URI的功能,判断两个URI是否指向同一资源。 - **安全操作**:Uri库遵循RFC3986标准,对URI进行编码和解码,防止...

    cdh6.3.2升级hive至3.x后登陆HiveServer2连接失败.doc

    Error: Could not open client transport with JDBC Uri: jdbc:hive2://hadoop105:10000: Failed to open new session: java.lang.IllegalArgumentException: Cannot modify hive.query.redaction.rules at runtime....

    soap协议规范.pdf

    在SOAP协议规范中,采用了特定的符号约定来描述协议的要求和建议,如“MUST”、“SHALL NOT”等,这些关键字的解释遵循RFC-2119标准。此外,还定义了一些名称空间前缀,如: - `SOAP-ENV`:与`...

    androdi ContentProvider和Uri详解

    在Android开发中,ContentProvider和Uri是两个至关重要的概念,它们是Android系统中不同应用程序间数据共享和交互的基础。本文将深入探讨这两个组件的工作原理、使用方法以及它们在实际开发中的应用。 首先,我们来...

    CXF 添加soap 头部信息.zip_CXF增加soap头验证_bluex8z_cxf_meltedkw7_soap信息头

    NodeList headers = header.getElementsByTagNameNS(CustomSoapHeader.NAMESPACE_URI, CustomSoapHeader.LOCAL_NAME); if (headers.getLength() &gt; 0) { CustomSoapHeader customHeader = (CustomSoapHeader) ...

    PHP SOAP DEMO

    `SoapClient`用于作为SOAP客户端,发起请求并接收响应,而`SoapServer`则用于创建SOAP服务,处理来自客户端的请求。 1. 创建SOAP服务: 要创建一个SOAP服务,首先需要定义服务接口,这通常是一个PHP类,包含了将要...

    soap协议规范

    同时,SOAP使用了一系列的名域前缀来关联特定的URI,例如“SOAP-ENV”与SOAP信封相关的URI关联,“SOAP-ENC”与编码规则关联,“xsi”与XSD实例规范关联,“xsd”与XSD规范关联。这些前缀有助于区分和组织XML文档中...

    soap白皮书 TXT格式

    ### SOAP白皮书核心知识点详解 #### 一、SOAP简介与功能 SOAP(Simple Object Access Protocol)是一种基于XML的信息交换协议,旨在为分布式系统提供一种标准化的远程过程调用(RPC)方式。它通过一系列规范定义了...

    博客《Uri详解之——Uri结构与代码提取》对应源码

    这个博客《Uri详解之——Uri结构与代码提取》的源码提供了深入理解Uri类的实例和实践,有助于开发者更好地理解和操作Uri。下面将详细探讨Uri的相关知识点。 1. **Uri基本概念**: Uri,全称Uniform Resource ...

Global site tag (gtag.js) - Google Analytics