- 浏览: 318577 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
huangyunbin:
swc.advance(); 这个什么时候被调用是最核心的 ...
滑动窗口计数java实现 -
80后的童年2:
深入浅出MongoDB应用实战开发网盘地址:https://p ...
MongoDB 从入门到精通专题教程 -
rryymmoK:
深入浅出MongoDB应用实战开发下载地址:http://pa ...
MongoDB 从入门到精通专题教程 -
u012352249:
怎么支持多个窗口啊?
滑动窗口计数java实现 -
rryymmoK:
深入浅出MongoDB应用实战开发百度网盘下载:链接:http ...
MongoDB 从入门到精通专题教程
HttpUrlConnection 发送 SOAP 请求,SAX 解析 SOAP 响应
并附上抓包工具:wireshark
并附上抓包工具:wireshark
/* * Socket远程调用Web服务实现,并用SAX解析XML文件,适用于性能要求较高场合 */ package com.mypack.soap.client; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.util.Stack; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; /** * 为了提高WebService调用效率,因此手Socket自已实现,无需依赖任何Soap引擎 * @version 1.0 * @date 2010-08-21 */ public final class SoapClientAsHttpUrlConnection { /** * apache 日志记录器,底层可切换实现 */ private static Log logger = LogFactory.getLog(SoapClientAsHttpUrlConnection.class); /** * 为了简单直接写了,最好单独写一方法并采用 Executors 启动线程,将返回值加入 BlockingQueue */ public static void main(String[] args) throws URISyntaxException, IOException, SAXException { // soap request string final String soapReuqest = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><ns1:queryUser soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://services.zhang.com\"><in0 xsi:type=\"ns2:UserInfo\" xsi:nil=\"true\" xmlns:ns2=\"http://tdo.zhang.com\"/></ns1:queryUser></soapenv:Body></soapenv:Envelope>"; // Web 服务所在的地址 URI uri = new URI("http://10.40.51.156:28888/testSoap/services/UserManage"); URL url = uri.toURL(); // 打开连接 HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(); // 可读取 httpUrlConnection.setDoInput(true); httpUrlConnection.setDoOutput(true); httpUrlConnection.setRequestMethod("POST"); // set request header httpUrlConnection.setRequestProperty("SOAPAction", ""); //httpUrlConnection.setRequestProperty("Content-Length", soapReuqest.length() + ""); OutputStream os = httpUrlConnection.getOutputStream(); PrintWriter out = new PrintWriter(os); out.println(soapReuqest); out.flush(); StringBuilder sb = new StringBuilder(); // http status ok if (HttpURLConnection.HTTP_OK == httpUrlConnection.getResponseCode()) { if (logger.isDebugEnabled()) { logger.debug("HTTP_OK"); } InputStream is = httpUrlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); for (String line = br.readLine(); line != null; line = br.readLine()) { sb.append(line); } is.close(); } // Release resource os.close(); out.close(); httpUrlConnection.disconnect(); String soapResponse = sb.toString(); System.out.println(soapResponse); // parse the soap response // 底层是xces解析器,效率较高 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); SimpleHandler handler = new SimpleHandler(); xmlReader.setContentHandler(handler); InputSource inputSource = new InputSource(new StringReader(soapResponse)); try { xmlReader.parse(inputSource); } catch (ExtSAXException e) { } // 便于演示,直接打出,生产环境请匆如此使用,可用日志进行 debug System.out.println("Response is " + handler.response); } /** * SAX 处理器,底层运用观察者模式 * 对于XML这种结构,运用栈进行存储 * 如果解析到目标串,利用抛出自定义异常来提前终止操作,提高效率 */ private static class SimpleHandler extends DefaultHandler { /** * 用栈缓存上下文信息 */ private final Stack<String> stack = new Stack<String>(); private String response; /** * 每次传递 16K 数据,这一点很重要,但是在这里只需要一个串,因此忽略 */ @Override public void characters(char[] ch, int start, int length) throws SAXException { if (!stack.empty() && "queryUserReturn".equals(stack.pop())) { response = new String(ch, start, length); // 抛出异常,结束解析 throw new ExtSAXException(); } } /** * 元素开始自动触发,将开始标签压栈缓存 */ @Override public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException { if ("queryUserReturn".equals(localName)) { stack.push(localName); } } } /** * 自定义异常,用于终止解析任务 */ private static class ExtSAXException extends SAXException { private static final long serialVersionUID = 1L; } }
- Wireshark_.zip.003.zip (2.3 MB)
- 下载次数: 117
- Wireshark_.zip.001.zip (9 MB)
- 下载次数: 97
- Wireshark_.zip.002.zip (9 MB)
- 下载次数: 91
评论
4 楼
yanliyun
2013-12-13
这几个案例也下载下来损害
3 楼
yanliyun
2013-12-13
楼主这个在我控制台输出 Response is null什么情况,能不能告诉详细怎么用的
2 楼
tojaoomy
2012-04-26
还需要在
httpUrlConnection.setRequestProperty("SOAPAction", "");
后添加httpUrlConnection.setRequestProperty("Content-type", "text/xml");
才会成功。
httpUrlConnection.setRequestProperty("SOAPAction", "");
后添加httpUrlConnection.setRequestProperty("Content-type", "text/xml");
才会成功。
1 楼
playingfly
2011-12-28
好文! 很经典!
发表评论
-
滑动窗口计数java实现
2016-02-20 13:13 11839滑动窗口计数有很多使用场景,比如说限流防止系统雪崩。相比计 ... -
面向对象程序设计思想(精华)
2014-11-12 15:52 1413面向对象语言具有封装,继承,多态的特征。那么在用面象对象语言 ... -
YY直播厅蠕虫病毒代码
2014-09-22 21:52 2458本来是可以直接通过<script>标签实现的, ... -
找出序列中不重复的元素
2014-09-17 21:51 1890阿里的一个面试题: 一个序列里除了一个元素,其他元素都会重复出 ... -
Apache Http Server Rewrite
2012-06-04 13:13 1131apache的rewrite功能很强大,详细参考:http:/ ... -
java操作MQ
2011-12-30 14:21 2833package mq; import jav ... -
实现动态验证码
2011-07-08 13:10 1155import java.awt.Color; imp ... -
简单统计代码行数
2010-12-30 17:34 1438真的很多,我刚写了个程序统计了一下,我们项目才695个类 并 ... -
采用MD5单向加密
2010-11-26 10:42 1268public static String get ... -
在报表中格式化货币
2010-11-25 10:01 1732最近在用FineReport这个工具进行系统的报表开发,发现在 ... -
Struts + JSP导出Excel报表
2010-11-07 20:21 2392据我所知 Java 导 Excel 报表有三种方法: 1, 在 ... -
Java正则实现EL表达式
2010-11-04 16:19 4451public static void main(Stri ... -
js格式化货币格式
2010-11-01 13:41 3771String.prototype.asCurren ... -
Hessian 发布服务及客户端实现
2010-10-19 13:36 2067服务接口: package com.test; pub ... -
java 项目中嵌入 jetty,并发布servlet
2010-10-13 11:47 2149package com.utan.tfs.jetty; ... -
TreeSet<T> 简单实现
2010-08-26 17:31 1518package com.mypack.ds; i ... -
SAX 解析 XML 实例
2010-08-21 14:32 2346xml文件: <?xml version=" ... -
利用 Spring 中的 Resource 读取文件和网络资源
2010-08-21 12:04 3750利用 Spring 中的 Resource 读取文件和网络资源 ... -
NIO SAX
2010-08-20 18:51 1037NIO与SAX 直接上教程 -
socket 发送 soap 请求
2010-08-19 23:46 3834package com.mypack.soap.client; ...
相关推荐
Java利用HttpURLConnection发送post请求上传文件
调用URL对象的openConnection( )来获取HttpURLConnection对象实例: HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 设置HTTP请求使用的方法:conn.setRequestMethod("GET"); 设置连接超时,...
httpurlconnection 获取服务器数据并解析
使用HttpURLConnection发送短信
使用HttpURLConnection请求链接,并解析View上的json数据字符串,并且使用Handler;使用JsonObject来解析数据
java实现多次HttpURLConnection共享session,发送两次请求共享同一个session,这样做爬虫的时候就可以爬网站登录后能看到的内容了
分别使用HttpURLConnection OKHttp实现请求
HttpURLConnection的(get请求、post请求)
android使用Java原生httpUrlConnection进行get请求
HttpURLConnection请求和HttpClient请求...................................................................
创建HTTP连接:使用Java.net包中的HttpURLConnection类来创建HTTP连接,从而可以发送POST请求并接收响应。 设置请求参数:设置请求参数,包括请求方法、请求头、请求体等信息。我们的示例代码中包括了常见的请求头...
主要介绍了java后台调用HttpURLConnection类模拟浏览器请求实例,该实例可用于接口调用,具有一定的实用价值,需要的朋友可以参考下
java网络请求工具类 HttpURLConnection post请求工具类HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setRequestMethod("POST");// 提交模式
请求http及数据返回解析,HttpURLConnection,HttpClient请求http及数据返回解析,HttpURLConnection,HttpClient
基于HttpURLConnection的封装http请求,进行了https校验忽略,对get和post进行了整合,更方便请求,对header以及超时的进行了可配置化处理,对返回也进行了封装,便于请求结果的处理。
NULL 博文链接:https://zjutsoft.iteye.com/blog/1313754
这个是我自己集成框架的时候写的一个HTTP请求的辅助类,主要是通过HttpURLConnection 来实现JAVA的HTTP请求,有的人会问、为什么有些网站发请求没问题,有些网站发请求却返回数据,在给网站发送请求的时候,请看清楚...
主要介绍了java HttpURLConnection 发送文件和字符串信息的相关资料,需要的朋友可以参考下
主要给大家介绍java利用原始httpUrlConnection发送post数据,设计到httpUrlConnection类的相关知识,感兴趣的朋友跟着小编一起学习吧
HttpUrlConnection 用Post方式提交请求 ,我找的不容易,需要的时候很重要