六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

使用HttpURLConnection类(利用sessionId保持会话

[复制链接]

升级  25.67%

79

主题

79

主题

79

主题

举人

Rank: 3Rank: 3

积分
277
 楼主| 发表于 2013-2-3 14:36:04 | 显示全部楼层 |阅读模式
TestServlet.javapackage nj.servlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class TestServlet extends HttpServlet {     public void doPost(HttpServletRequest request,HttpServletResponse response){         HttpSession session=request.getSession();         String str=(String)session.getAttribute("name");         System.out.println("name:"+str);         session.setAttribute("name", "nj");         /*String ss=request.getHeader("Cookie");         System.out.println("session id:"+ss);*/     }     public void doGet(HttpServletRequest request,HttpServletResponse response){         this.doPost(request, response);     } }  TestServlet.javapackage nj.ws.test; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.Map; import java.util.Set; public class TestConnection {          public static void main(String[] args){         //第一次连接,返回session id         String sessionId=testUrlConnection();         //第二次使用session id连接         connectionBySession(sessionId);     }          public static String testUrlConnection(){         String urlStr="Http://localhost:8080/WebServiceProject/testServlet";         OutputStream out=null;         InputStream in=null;         try {             URL url=new URL(urlStr);             HttpURLConnection con=(HttpURLConnection)url.openConnection();             con.setRequestMethod("POST");                          //打印请求头信息             Map hfs=con.getHeaderFields();             Set<String> keys=hfs.keySet();             for(String str:keys){                 List<String> vs=(List)hfs.get(str);                 System.out.print(str+":");                 for(String v:vs){                     System.out.print(v+"\t");                 }                 System.out.println();             }             System.out.println("-----------------------");             String cookieValue=con.getHeaderField("Set-Cookie");             System.out.println("cookie value:"+cookieValue);             String sessionId=cookieValue.substring(0, cookieValue.indexOf(";"));             return sessionId;         } catch (Exception e) {             e.printStackTrace();             return null;         }finally{             try {                 if(in!=null)                     in.close();                 if(out!=null)                     out.close();             } catch (IOException e) {                 e.printStackTrace();             }         }     }          public static void connectionBySession(String sessionId){         InputStream in=null;         try{             String url="Http://localhost:8080/WebServiceProject/testServlet";             URL u=new URL(url);             HttpURLConnection con=(HttpURLConnection)u.openConnection();             con.setRequestMethod("POST");             con.setRequestProperty("Cookie", sessionId);             in=con.getInputStream();         }catch(Exception e){             e.printStackTrace();         }finally{             try {                 if(in!=null)                     in.close();             } catch (IOException e) {                 e.printStackTrace();             }         }     } } 运行后打印结果:Web服务端:name:nullname:nj 测试类打印:null:HTTP/1.1 200 OK Content-Length:0 Set-Cookie:JSESSIONID=7827696933343BC5005E5244369CB2A8; Path=/WebServiceProject Date:Mon, 01 Dec 2008 03:16:30 GMT Server:Apache-Coyote/1.1 -----------------------cookie value:JSESSIONID=7827696933343BC5005E5244369CB2A8; Path=/WebServiceProject
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表