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来实现。 具体的看代码如上:

 

你可能感兴趣的:(java,http,webservice,SOAP,aixs)