HttpURLConnection 进行WebService请求

package com.soap.client;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * WebService 发起请求
 * 
 * @author Roger
 */
public class SoapRequestTest {

	public static void main(String[] args) throws Exception {

		// Web Service 请求参数
		StringBuffer soapMessage = new StringBuffer();
		soapMessage.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
		soapMessage.append("<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/\">");
		soapMessage.append("  <soap:Body>");
		soapMessage.append("    <GetBasicInfo xmlns=\"http://www.tech-trans.com.cn/\" />");
		soapMessage.append("  </soap:Body>");
		soapMessage.append("</soap:Envelope>");

		String soapAction = "http://www.tech-trans.com.cn/GetBasicInfo";

		// 进行soap请求并获取响应信息
		String response = soapRequest(soapMessage.toString(), soapAction);
		System.out.println(response);
	}

	/**
	 * Web Service 请求
	 * 
	 * @param soapMessage
	 *            请求参数
	 * @param soapAction
	 *            请求soapAction
	 * @return 响应XML
	 */
	public static String soapRequest(String soapMessage, String soapAction) {
		// 请求响应报文
		StringBuilder response = new StringBuilder();

		HttpURLConnection conn = null;
		try {
			conn = getConnection();
			// 设置请求Header
			conn.setRequestProperty("Content-Length",
					String.valueOf(soapMessage.length()));
			conn.setRequestProperty("SOAPAction", soapAction);

			// request输出流
			OutputStream output = conn.getOutputStream();
			if (null != soapMessage) {
				byte[] b = soapMessage.getBytes("utf-8");
				// 发送soap请求报文
				output.write(b, 0, b.length);
			}
			output.flush();
			output.close();

			// 获取soap响应报文
			InputStream input = conn.getInputStream();

			getResponseMessage(input, response);

			input.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			conn.disconnect();
		}
		return response.toString();
	}

	/**
	 * 获取响应数据,进行转码,处理乱码
	 * 
	 * @param input
	 *            InputStream
	 * @param response
	 *            响应数据
	 * @throws Exception
	 */
	public static void getResponseMessage(InputStream input,
			StringBuilder response) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(input,
				"UTF-8"));
		int c = -1;
		// sb为返回的soap响应报文字符串
		while (-1 != (c = br.read())) {
			response.append((char) c);
		}
	}

	/**
	 * 获取HttpURLConnection
	 * 
	 * @return HttpURLConnection
	 * @throws Exception
	 */
	public static HttpURLConnection getConnection() throws Exception {
		// 设置soap请求报文的相关属性
		// 服务端WSDL
		URL url = new URL("http://192.168.1.1:1999/CRM_VIP_Proxy.asmx?WSDL");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 设置请求Header
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setDefaultUseCaches(false);
		conn.setRequestProperty("Host", "58.42.235.140");
		conn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
		conn.setRequestProperty("POST", "/CRM_VIP_Proxy.asmx HTTP/1.1");
		return conn;
	}

}
时间: 2024-07-31 19:43:14

HttpURLConnection 进行WebService请求的相关文章

Java使用HttpURLConnection调用WebService(原始方法)

说明:使用Java原生的HttpURLConnection调用WebService可以免去引入SOA的框架,比如一些CXF框架等.可以使代码足够精简,比如对于一些只调用一两个接口的,这种方式是最适合的. package com.jsfot.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWrit

使用httpclient实现http链接池与使用HttpURLConnection发送http请求的方法与性能对比

使用httpclient实现http链接池与使用HttpURLConnection发送http请求的方法与性能对比 在项目中需要使用http调用接口,实现了两套发送http请求的方法,一个是使用apache的httpclient提供的http链接池来发送http请求,另一个是使用java原生的HttpURLConnection来发送http请求,并对两者性能进行了对比. 使用httpclient中的链接池发送http请求 使用最新的4.5.2版httpclient进行实现.在maven中引入 <

kettle的HTTPPOST控件发送WSDL的webservice请求配置

1.webservice请求的URL:http://pubservice.rjhn.com.cn/AppserviceTest/JsonWcfService.svc?WSDL 2.使用SOAPUI测试接口 3.使用kettle测试接口 1)WSDL:网络服务描述语言是Web Service的描述语言,它包含一系列描述某个web service的定义.WSDL 用来描述如何访问具体的接口 2)SOAP:soap用来描述传递信息的格式,SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传

HttpUrlConnection发送url请求(后台springmvc)

1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "http://localhost:8080/dsdemo/"; public static String userToken = null; public static String problemName = null; public static String sendPost(String su

android菜鸟学习笔记24----与服务器端交互(一)使用HttpURLConnection和HttpClient请求服务端数据

主要是基于HTTP协议与服务端进行交互. 涉及到的类和接口有:URL.HttpURLConnection.HttpClient等 URL: 使用一个String类型的url构造一个URL对象,如: URL url = new URL(http://10.0.2.2/index.php); openConnection()方法返回一个对指定url的资源的连接.返回类型是URLConnection,但是,由于这里我们一般用的是http协议,所以返回的实际是HttpURLConnection对象,故一

使用Jquery Ajax的webservice请求来实现更简练的Ajax

转载:http://blog.csdn.net/lovecruel/article/details/6697764 这里对Jquery的Ajax 调用WebService 几个参数做一下简单的说明, type:请求的类型,这里必须用post .WebMethod方法只接受post类型的请求 contentType:发送信息至服务器时内容编码类型.我们这里一定要用application/json url:请求的服务器端处理程序的路径,格式为"文件名(含后缀)/方法名" data:参数列表

HttpUrlConnection发起POST请求

1 StringBuffer str=new StringBuffer(); 2 Map<String,Object> requestParamsMap = new HashMap<String, Object>(); 3 requestParamsMap.put("type",0);//添加绑定参数 4 Iterator it=requestParamsMap.entrySet().iterator();//获取迭代器 5 while(it.hasNext()

webService请求方式快速生成代码 (Postman)

Postman 这个东西只能在外网下载,是Google一个插件. 1.要到外网,这里就不具体介绍怎么去外网了. 2.上到谷歌浏览器,找到更过工具--->扩张程序--->获取更多扩张程序 3.在搜索框中输入 Postman ,找到后添加到CHROME即可 [我这里是添加过了] 4.打开这个软件,他会自己在你桌面生成一个客户端[接下来才是重点] 输入webService URL ,如果有数据就填写数据,选择的请求类型,点击send ,即可放回参数 5.你以为这样就结束了吗?这个时候才是偷懒的时候.

Android(或者Java)通过HttpUrlConnection向SpringMVC请求数据(数据绑定)

问题描述 当我们使用SpringMVC作为服务端的框架时,有时不仅仅要应对web前端(jsp.javascript.Jquery等)的访问请求,有时还可能需要响应Android和JavaSE(桌面应用)这些客户端的请求,因此,除了web使用form表单或者ajax作为客户端获取Controller响应之外,纯Java语言向SpringMVC的Controller提供参数和请求结果也是必须要实现的.web前端使用form和ajax来获取Controller响应在本篇博客暂不深究,本篇博课着力于实现