- 浏览: 215049 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (144)
- Python (6)
- Java (15)
- Project management (2)
- DB (11)
- Spring (1)
- Mobile (3)
- 互联网 (10)
- Maven (2)
- SCM (5)
- linux (24)
- Mac (14)
- UCD / UED (6)
- Tools (1)
- Test (1)
- iPhone (1)
- 新产品&新工具 (8)
- OAuth (4)
- Java Script (5)
- HTML5 (2)
- Lucene / Solr (7)
- nginx (1)
- Product Manager (1)
- Design (1)
- Office (1)
- RegExp (0)
- 性能调优 (2)
- 读书笔记 (2)
- NodeJs (2)
最新评论
-
410163269:
看不清楚 蛋疼
基于 OAuth 安全协议的 Java 应用编程 -
xufun:
路过,拜读学习了。谢谢!
未来的授权标准 -- OAuth 2.0 -
xufun:
好文!路过拜读了,谢谢!
NoSQL - CouchDB入门 -
mimicom:
牛b......
最牛B的 Linux Shell 命令(三) -
as3291363:
你有一些 中文資料嗎????
Java Script 代码生成器: CoffeeScript
* 1,在HTTP的WEB应用中, 应用客户端和服务器之间的状态是通过Session来维持的, 而Session的本质就是Cookie,
* 简单的讲,当浏览器向服务器发送Http请求的时候, HTTP服务器会产生一个SessionID,这个SessionID就唯一的标识了一个客户端到服务器的请求会话过程.
* 就如同一次会议开始时,主办方给每位到场的嘉宾一个临时的编号胸牌一样, 可以通过这个编号记录每个嘉宾(客户端)的活动(请求状态).
* 为了保持这个状态, 当服务端向客户端回应的时候,会附带Cookie信息,当然,Cookie里面就包含了SessionID
* 客户端在执行一系列操作时向服务端发送请求时,也会带上这个SessionID, 一般来说,Session也是一个URL QueryParameter ,就是说,session可以以Key-Value的形式通过URL传递
* 比如,http://www.51etest.com/dede/login.php?PHPSESSIONID=7dg3dsf19SDf73wqc32fdsf
* 一般而言,浏览器会自动把此Session信息放入Header报文体中进行传递.
* 如果浏览器不支持Cookie,那么,浏览器会自动把SessionID附加到URL中去.
*
* 2,在这个例子中,以登陆这个功能点进行讲解.
* 首先,我们登陆的页面是http://www.51etest.com/dede, 我们第一次访问这个页面后,可以从服务器过来的Http Response报文中的Header中找出服务器与浏览器向关联的数据 -- Cookie,
* 而且Session的值也在Cookie中. 于是,我们可以通过分析Set-Cookie这个Header中的参数的值,找到Seesion的Key-Value段.
* 然后,我们再向服务器发送请求,请求URL为:post@@http://www.51etest.com/dede/login.php@@userid=admin&pwd=tidus2005&gotopage=/dede/&dopost=login
* 服务器验证登陆成功了, 并且在此次会话变量中增加了我们登陆成功的标识.
*
* 3,增加一个广告定义
* 增加一个广告定义其实就是一个添加数据的过程,无非是我们把我们要添加的数据通过参数的形式告诉指定url页面,页面获取后添加到数据库去而已.
* 此url地址为:
* post@@http://www.51etest.com/dede/ad_add.php@@dopost=save&tagname=test&typeid=0&adname=test&starttime=2008-05-29
* 因为这个页面会先判断我是否登陆
* 而判断的依据,前面讲了,就是根据我请求时的SessionID找到指定的Session数据区中是否存在我的登陆信息,
* 所以我当然要把访问登陆页面时获取的SessionID原封不动的再发回去
* 相当于对服务器说,这是我刚刚来时,你发我的临时身份证,我现在可以形势我的权利。
*
* 这就是整个Java后台登陆网站,然后添加数据的过程。
* 简单的讲,当浏览器向服务器发送Http请求的时候, HTTP服务器会产生一个SessionID,这个SessionID就唯一的标识了一个客户端到服务器的请求会话过程.
* 就如同一次会议开始时,主办方给每位到场的嘉宾一个临时的编号胸牌一样, 可以通过这个编号记录每个嘉宾(客户端)的活动(请求状态).
* 为了保持这个状态, 当服务端向客户端回应的时候,会附带Cookie信息,当然,Cookie里面就包含了SessionID
* 客户端在执行一系列操作时向服务端发送请求时,也会带上这个SessionID, 一般来说,Session也是一个URL QueryParameter ,就是说,session可以以Key-Value的形式通过URL传递
* 比如,http://www.51etest.com/dede/login.php?PHPSESSIONID=7dg3dsf19SDf73wqc32fdsf
* 一般而言,浏览器会自动把此Session信息放入Header报文体中进行传递.
* 如果浏览器不支持Cookie,那么,浏览器会自动把SessionID附加到URL中去.
*
* 2,在这个例子中,以登陆这个功能点进行讲解.
* 首先,我们登陆的页面是http://www.51etest.com/dede, 我们第一次访问这个页面后,可以从服务器过来的Http Response报文中的Header中找出服务器与浏览器向关联的数据 -- Cookie,
* 而且Session的值也在Cookie中. 于是,我们可以通过分析Set-Cookie这个Header中的参数的值,找到Seesion的Key-Value段.
* 然后,我们再向服务器发送请求,请求URL为:post@@http://www.51etest.com/dede/login.php@@userid=admin&pwd=tidus2005&gotopage=/dede/&dopost=login
* 服务器验证登陆成功了, 并且在此次会话变量中增加了我们登陆成功的标识.
*
* 3,增加一个广告定义
* 增加一个广告定义其实就是一个添加数据的过程,无非是我们把我们要添加的数据通过参数的形式告诉指定url页面,页面获取后添加到数据库去而已.
* 此url地址为:
* post@@http://www.51etest.com/dede/ad_add.php@@dopost=save&tagname=test&typeid=0&adname=test&starttime=2008-05-29
* 因为这个页面会先判断我是否登陆
* 而判断的依据,前面讲了,就是根据我请求时的SessionID找到指定的Session数据区中是否存在我的登陆信息,
* 所以我当然要把访问登陆页面时获取的SessionID原封不动的再发回去
* 相当于对服务器说,这是我刚刚来时,你发我的临时身份证,我现在可以形势我的权利。
*
* 这就是整个Java后台登陆网站,然后添加数据的过程。
package cn.javadr.product; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; /** * HttpClient * * @author Tang Ren email: <a * href="mailto:tangren1206@163.com">tangren1206@163.com</a> * * <A href="mailto:post@@http://www.51etest.com/dede/login.php@@userid=xxxx&pwd=xxxxxx&gotopage=/dede/&dopost=login">post@@http://www.51etest.com/dede/login.php@@userid=xxxx&pwd=xxxxxx&gotopage=/dede/&dopost=login </A> * * post@@http://www.51etest.com/dede/ad_add.php@@dopost=save&tagname=test&typeid=0&adname=test&starttime=2008-05-29 * 20:58:25&endtime=2008-06-28 20:58:25×et=0&normbody=test&expbody=test * */ /** * */ public class HttpClient { private static final String USER_AGENT_VALUE = "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)"; /** * Cmd splitor default is symbol '$$'. */ private static final String HTTP_CLIENT_CMD_SPLITOR = "@@"; /** * Post parameter splitor default is symbol '&'. */ private static final String POST_PARAMETER_SPLITOR = "&"; private static final String POST_PARAMETER_KV_SPLITOR = "="; private String cookie = null; private Map cookieMap = new HashMap(); public static void main(String[] args) { HttpClient client = new HttpClient(); } public HttpClient() { // Input http request url BufferedReader consleReader = new BufferedReader(new InputStreamReader( System.in)); String httpResponse = null; String url = null; String cmd = null; String method = null; try { while (true) { cmd = consleReader.readLine(); if (cmd.indexOf(HTTP_CLIENT_CMD_SPLITOR) == -1) continue; method = cmd.split(HTTP_CLIENT_CMD_SPLITOR)[0]; url = cmd.split(HTTP_CLIENT_CMD_SPLITOR)[1]; if (method.toUpperCase().equals("GET")) { httpResponse = this.getMethod(url, true); } else if (method.toUpperCase().equals("POST")) { Map parameters = this.parsePostParameters(cmd .split(HTTP_CLIENT_CMD_SPLITOR)[2]); httpResponse = this.postMethod(url, parameters, true); } System.out.println(httpResponse); } } catch (IOException e) { e.printStackTrace(); } finally { try { consleReader.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Request specifid url with 'GET' method. And return HTTP response content. * * @param url * @return */ private String getMethod(String url, boolean keepCookie) { if (url == null || url.length() == 0) { return "Requst url could not be null or empty."; } StringBuffer result = new StringBuffer(); try { HttpURLConnection httpURLConnection = this.getHttpURLConnection( url, keepCookie); // Set request properties. this.settingHttpRequestHeader(httpURLConnection); httpURLConnection.setRequestMethod("GET"); // Getting or setting cookie this.gettingOrSettingCookie(httpURLConnection, keepCookie); InputStream httpInputStream = httpURLConnection.getInputStream(); BufferedReader httpBufferedReader = new BufferedReader( new InputStreamReader(httpInputStream, "GBK")); result.append(this.readBufferedContent(httpBufferedReader)); // Connect to host. httpURLConnection.connect(); } catch (IOException e) { e.printStackTrace(); return "getHttpURLConnection failed."; } return result.toString(); } public String postMethod(String url, Map parameters, boolean keepCookie) { StringBuffer httpResponse = new StringBuffer(); HttpURLConnection httpURLConnection = null; OutputStream httpOutputStream = null; try { httpURLConnection = this.getHttpURLConnection(url, keepCookie); // Set request properties. this.settingHttpRequestHeader(httpURLConnection); // Set request method with 'POST' httpURLConnection.setRequestMethod("POST"); // Set connection output is true. httpURLConnection.setDoOutput(true); // Getting or setting cookie this.gettingOrSettingCookie(httpURLConnection, keepCookie); // Get Http output stream httpOutputStream = httpURLConnection.getOutputStream(); // Build post parameters string StringBuffer postParams = new StringBuffer(); int index = 0; for (Iterator<Entry> iter = parameters.entrySet().iterator(); iter .hasNext(); index++) { Entry<String, String> entry = iter.next(); postParams.append(index != 0 ? "&" : ""); postParams.append(entry.getKey()); postParams.append("="); postParams.append(entry.getValue()); } httpOutputStream.write(postParams.toString().getBytes()); BufferedReader httpBufferedReader = new BufferedReader( new InputStreamReader(httpURLConnection.getInputStream())); httpResponse.append(this.readBufferedContent(httpBufferedReader)); } catch (IOException e) { e.printStackTrace(); return null; } finally { try { httpOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return httpResponse.toString(); } /** * Setting HTTP request header properties * * @param httpURLConnection */ private void settingHttpRequestHeader(HttpURLConnection httpURLConnection) { if (httpURLConnection == null) return; httpURLConnection.setRequestProperty("User-Agent", USER_AGENT_VALUE); // TODO setting some other properties here . . . } /** * Get HttpURLConnection by specified url string. * * @param url * @return * @throws IOException */ private HttpURLConnection getHttpURLConnection(String url, boolean keepCookie) throws IOException { URL urlObj = new URL(url); URLConnection urlConnection = urlObj.openConnection(); if (urlConnection instanceof HttpURLConnection) return (HttpURLConnection) urlConnection; throw new MalformedURLException(); } /** * Read bufferedReader buffered content. * * @param bufferedReader * @return */ private String readBufferedContent(BufferedReader bufferedReader) { if (bufferedReader == null) return null; StringBuffer result = new StringBuffer(); String line = null; try { while ((line = bufferedReader.readLine()) != null) { result.append(line); } } catch (IOException e) { e.printStackTrace(); return null; } return result.toString(); } /** * Parse and create parameter map with parameter string * * @param parameterString * @return */ private Map parsePostParameters(String parameterString) { if (parameterString == null || parameterString.length() == 0) return null; Map result = new HashMap<String, String>(); // only one parameter key-value pair if (parameterString.indexOf(POST_PARAMETER_SPLITOR) == -1) { if (parameterString.indexOf(POST_PARAMETER_KV_SPLITOR) != -1) result.put(parameterString.split(POST_PARAMETER_KV_SPLITOR)[0], parameterString.split(POST_PARAMETER_KV_SPLITOR)[1]); } else { String[] keyValues = parameterString.split(POST_PARAMETER_SPLITOR); for (int i = 0; i < keyValues.length; i++) { String keyValue = keyValues[i]; result.put(keyValue.split(POST_PARAMETER_KV_SPLITOR)[0], keyValue.split(POST_PARAMETER_KV_SPLITOR)[1]); } } return result; } /** * Get or set cookie. * * @param httpURLConnection * @param keepCookie */ private void gettingOrSettingCookie(HttpURLConnection httpURLConnection, boolean keepCookie) { // Getting or setting cookie. if (cookie == null || cookie.length() == 0) { String setCookie = httpURLConnection.getHeaderField("Set-Cookie"); cookie = setCookie.substring(0, setCookie.indexOf(";")); } else if (keepCookie) { httpURLConnection.setRequestProperty("Cookie", cookie); } } }
发表评论
-
Java持续集成
2011-03-18 17:55 1482Hudson参考:http://jdonee.iteye. ... -
发布 IK Analyzer 3.2.8 for Lucene3.X
2011-03-07 13:06 1044IK Analyzer 3.2.8版本修订 1.优化了英文字 ... -
常用Java工具类
2010-10-29 14:31 1027Ignacio Coloma 说道:作为 ... -
Google 将 Instantiations 制作的 JAVA 开发工具全部免费放出
2010-09-19 10:46 841Google于今年8月买下了JAVA/AJAX工具开发商I ... -
hsqldb使用
2010-08-18 16:11 1079hsqldb有下面几种模式 1.Server模式 首先 ... -
JAVA基础:什么是JMS(Java消息服务)
2010-08-17 10:31 892在不同系统之间交换信息的一大障碍是如何在精确交换和格式化数据方 ... -
Open-Source Office Automation 权限管理
2010-08-11 17:50 18141 博客: Metadmin--http://metad ... -
How to remote debug with Eclipse
2010-01-28 14:47 10401. 配置Tomcat支持remote debugging ... -
Visual JVM - 一个帮助我们查看JVM线程、对象等的方便工具
2009-07-20 16:50 3198今天看到blogjava上有人 ... -
Debugging with the Maven Jetty Plugin in Eclipse
2009-07-20 15:14 969Debugging with the Maven Jetty ... -
查看Java平台环境的代码
2009-07-13 14:29 1339public static void main(Strin ... -
Java模拟HTTP的Get和Post请求
2009-07-13 14:27 1612import org.apache.commons.htt ... -
Java模拟HTTP的Get和Post请求
2009-07-13 14:27 1948import org.apache.commons.htt ... -
JSON-lib使用
2009-03-18 15:20 1046FAQ: * 把bean转化为json 格式时老提示如下 ...
相关推荐
java后台请求http,区分get和post,一般是用来抓取带有登陆权限网站的数据,后台先登录,保持session,再请求数据URL
java中使用session模拟用户登陆
java解析的源码和小程序的源码,自己从项目中剥离出来的,肯定能用,有疑问私信,微信小程序获取各类信息(openid、session_key以及用户基本信息、解密手机号)
因为小程序原生不支持Cookie,因此也不支持Session。 网上找到的的一些方法有缺陷,而且很多累赘,估计没有实际测试过,在此直接给出实测可用的代码。 大概思路就是借助小程序本地储存+网络请求的header可读可写来...
strus2.0获取request,session的封装例子strus2.0获取request,session的封装例子
//使用POST提交 hc.addRequestProperty("Cookie", getSessionId()); hc.setDoOutput(true); hc.connect(); OutputStream out = hc.getOutputStream(); //参数是a=""&b=""这样拼接的一个串 out....
JAVA的hibernate手动获取session的方法
微信小程序的用户信息获取需要请求微信的服务器,通过小程序提供的API在小程序端获取CODE,然后将CODE传入到我们自己的服务器,用我们的服务器来换取session_key和openid。 小程序端比较简单,从教程的API部分把...
JAVA通过Session和Cookie实现网站自动登录的技术
java后台请求http,区分get和post,一般是用来抓取带有登陆权限网站的数据,后台先登录,保持session,再请求数据URL
微信小程序根据wx.login得到的code从Csharp后台获取openid和session_key(后台实现了4种方式调用jscode2session)
2.修改题目1,将提交的“用户名信息”保存到request中,“密码信息”保存到session中,而“用户是否已经注册的判定信息”保存在application中,并在显示页面中,分别从request、session和application中获取数据并...
struts2中request和session的获取
在普元Primeton EOS运算逻辑中获取HttpSession和Httprequest以及application 在普元Primeton EOS运算逻辑中获取HttpSession和Httprequest以及application
$res = (new WeixinApp())->decryptData( $encryptedData, $iv, &$data,$session_key ) (参数说明:$encryptedData string 加密的用户数据 小程序端才能获取到 。$iv string 与用户数据一同返回的初始向量小程序端...
session,request之间的区别,session在系统登录之后系统自动产生,request只能在页面之间存在
struts2 获取request session对象
JSP获取HTTP header信息(request)例子JSP获取HTTP header信息(request)例子
javascriptSession用户session共享页面数据,适合于单页面应用程序开发
android获取web服务器端session并验证登陆