会话对象返回null [英] session object return null

查看:153
本文介绍了会话对象返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java(客户端应用程序)发布html请求,该请求执行简单的登录任务,将登录变量存储在会话中,在登录后将程序重定向到另一个页面(default2 ),在加载default2页面时检查会话,但发现它为null! ,帮助:omg:(Java不是Web主机,而是j2se程序)
Web应用程序是asp

I''m trying to use java (client application) to post an html request , the request perform a simple login task , the login variable is stored in the session , after the login the program is redirected to another page (default2) ,on load the default2 page check the session , but it finds it null! , help :omg: ( the Java isn''t a web host , its a j2se program )
the web application is asp

try {
    URL u = new URL("http://localhost:51132/WebSite5/Default.aspx?name=user&pass=password");
    HttpURLConnection uc = (HttpURLConnection) u.openConnection( );
    int code = uc.getResponseCode( );
    String response = uc.getResponseMessage( );
    System.out.println("HTTP/1.x " + code + " " +response);
for (int j = 1; ; j++) {
    String header = uc.getHeaderField(j);
    String key = uc.getHeaderFieldKey(j);
   if (header == null || key == null) break;
    System.out.println(uc.getHeaderFieldKey(j) + ": " +header);
    }
    InputStream in = new
    BufferedInputStream(uc.getInputStream( ));
    Reader r = new InputStreamReader(in);
    int c;
while ((c = r.read( )) != -1) {
    System.out.print((char) c);
   
    }
  
  }
catch (MalformedURLException ex) {
    System.err.println(args[0] + " is not a parseable URL");
}
catch (IOException ex) {
System.err.println(ex);
}

}



default.aspx的代码,这是我用于登录的页面



the code of default.aspx which is the page i use for login

protected void Page_Load(object sender, EventArgs e)
    {
        string name = Page.Request.QueryString["name"];
        string pass = Page.Request.QueryString["pass"];
        if (name.CompareTo("user") == 0 && pass.CompareTo("password") == 0) {
            Session["loged"]= "yes";
            Response.Redirect("Default2.aspx");
        }
    }


我在其中检查会话变量的default2.aspx页的代码


the code of default2.aspx th page where i check the session variable

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["loged"] == null) {
            Response.Redirect("error.aspx");
        }
    }

推荐答案

首先,现在实现此方法的方式有缺陷,因为您在url中同时输入了登录名和密码,这绝对不是安全的!

您现在正在做的事情是在中间某个地方开始设置会话.您还需要进一步了解,此时您的客户是谁?在您提供的代码中,它将始终是执行Java代码的服务器.也许这就是主意,但也许不是.

解决此问题的最佳方法是创建一个简单的asp页面,您可以在其中登录并可以用来测试登录是否以这种方式成功.然后,您可以从那里走更远.

祝你好运!
First of all, the way you''re implementing this now is flawed because you give both login and password in the url, This is absolutely not secure!

What you now are doing is setting up the session beginning somewhere in the middle. Further you have to understand, who is your client at this point? In the code you gave this would always be the server that executes the java code. Maybe this is the idea, but maybe not.

The best way to solve this is to create a simple asp page where you can login and can use to test if the login succeeds this way. You can then go further from there.

Good luck!


无法在asp和ASP.NET之间维护会话,我非常怀疑该会话是否可能存在于java页面和aspx之间. .怎么会呢?会话存储在Web主机中的服务器上,并且您正在使用两个不同的Web主机和两个不同的框架.人们使用asp和ASP.NET解决此问题的方法是创建一个Windows服务,该服务获取会话ID(两者之间相同),并使用该ID读取和写入会话值以在两者之间传递.如果Java可以访问Windows会话,则需要执行相同的操作.如果没有,那么您就被搞砸了.

当然,您可以在URL上传递数据,但是如前所述,对于任何需要安全保护的东西,您都不会使用它.将其发布到页面上也不安全,来自客户端的任何内容都可能被篡改.
There is no way to maintain session between asp and ASP.NET, I doubt very much that it''s possible for the session to exist between a java page and an aspx. How can it ? The session is stored ON THE SERVER, in the web host, and you''re using two different web hosts, and two different frameworks. The way people got around this with asp and ASP.NET was to create a windows service which took the session id ( which is the same between the two ) and used that to read and write session values to pass between the two. If java can access a windows session, you will need to do the same. If not, you''re screwed.

Of course you CAN pass data on the URL, but as has been noted, this is not something you''d use for anything that needs to be secure. Posting it to the page is also not secure, anything that comes from the client, can be tampered with.


这篇关于会话对象返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆