2012-09-14 52 views
0

在我的android应用程序中,第一个屏幕中的用户身份验证和servlet回复带有会话ID ... 第二个屏幕中用户再次调用带有会话ID的servlet在URL中所加入; jsession =“会话ID”与参数一起...添加问题; android中的HttpURLconnection中的jsessionid用于GET请求

但servlet不是获取会话ID,并作为响应未经验证的新会话...

哪里是问题?

我使用此代码为连接

    URL u = new URL(aurl[0]); 
     url=aurl[0]; 
     publishProgress(""+20,"Connecting..."); 
     HttpURLConnection c = (HttpURLConnection) u.openConnection(); 
     publishProgress(""+40,"Connecting..."); 
     c.setRequestMethod("GET"); 
     c.setDoOutput(true); 
     c.connect(); 

     publishProgress(""+45,"Connecting..."); 
     publishProgress(""+40,aurl[0]); 
     int lenghtOfFile = c.getContentLength(); 
     System.out.println(lenghtOfFile+"lenghtOfFile"); 

     is = c.getInputStream(); 
+0

问题似乎是;在url JSESSIONID =的sessionid它显示Filenotfound例外,因为其在IS = c.getInputStream();如何解决此问题... –

+0

显示您的网址以及如何添加jsession –

+0

http:// ip/servletname:jsessionid = sessionid?参数 –

回答

0

中从来没有做过的URL添加jseesioid .. 代码使用此解决方案

httpsessionmanagement

尤其是这两个工作...

// Gather all cookies on the first request. 
URLConnection connection = new URL(url).openConnection(); 
List<String> cookies = connection.getHeaderFields().get("Set-Cookie"); 
// ... 

// Then use the same cookies on all subsequent requests. 
connection = new URL(url).openConnection(); 
for (String cookie : cookies) { 
connection.addRequestProperty("Cookie", cookie.split(";", 2)[0]); 
} 
// ... 
2

我面临同样的问题,并找到一个简单的解决方案。

//首先设置默认的cookie管理器。

CookieHandler.setDefault(new CookieManager(null,CookiePolicy.ACCEPT_ALL));

//以下所有后续URLConnections将使用相同的Cookie管理器。

URLConnection connection = new URL(url).openConnection();

// ...

连接=新的网址(URL).openConnection();

// ...

连接=新的网址(URL).openConnection();

// ...

这是我找到的最简单的解决方案。我们需要设置默认的CookieManager。 Using java.net.URLConnection to fire and handle HTTP requests是伟大的博客。

由于