现在的位置: 首页 > 综合 > 正文

java 客户端直接读取Cookies

2013年09月11日 ⁄ 综合 ⁄ 共 788字 ⁄ 字号 评论关闭
	public String getClientCookies() throws Exception {
/*		
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost("http://10.11.77.18:1230/count");
		Header[] myheader=httpPost.getAllHeaders();
		StringBuffer sb = new  StringBuffer();
		for (int i=0;i<myheader.length;i++){
			sb.append(myheader[i]);
			System.out.println(myheader[i]);
		}
		//sb.toString();
 
		System.out.println(httpPost.getAllHeaders());
		//return httpPost.getAllHeaders().toString();
		return sb.toString();*/
		URL url=new URL("http://10.11.77.18:1230/count");
		
		
		HttpURLConnection huc= (HttpURLConnection) url.openConnection(); 
		InputStream is = huc.getInputStream(); 
		// 取得sessionID. 
		String cookieVal = huc.getHeaderField("Set-Cookie"); 
		String sessionId="1"; 
		if(cookieVal != null) 
		{ 
		    sessionId = cookieVal.substring(0, cookieVal.indexOf(";")); 
		    return sessionId;
		}
		
		return sessionId;

抱歉!评论已关闭.