HttpURLConnection 进行WebService请求
2016-06-30 11:45
519 查看
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; } }
相关文章推荐
- RPC failed; result=22, HTTP code = 411
- HTTP Header 属性列表
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- 如何在 Linux 中快速地通过 HTTP 提供文件访问服务
- 深入HTTP head的使用详解
- Ruby程序中发送基于HTTP协议的请求的简单示例
- ASP 中使用 HTTP 协议发送参数详解
- C#基于socket模拟http请求的方法
- http www安全必备知识
- asp HTTP 500错误 常见问题分析
- http代理相关知识分析
- 在Node.js中使用HTTP上传文件的方法
- php错误提示failed to open stream: HTTP request failed!的完美解决方法
- php提示Failed to write session data错误的解决方法