j2me连接到弹簧安全

时间:2012-10-16 16:22:58

标签: post java-me spring-security

我有一个使用spring security进行身份验证的应用程序 我试图从j2ME midlet连接到这个应用程序

所以我使用post方法从j2me应用程序发送HTTP请求以发送用户名和密码,但它不起作用

后期方法代码很好。它适用于同一应用程序的其他服务(不需要验证)

但是当涉及到身份验证时,我得到了响应代码302

这是我的代码

        OutputStream os = null;
        HttpConnection connection = null;
        InputStream is = null;
        StringBuffer sb = new StringBuffer();
        try {
                connection = (HttpConnection)Connector.open("http://localhost:8080/myAppli/j_spring_security_check");
                connection.setRequestMethod(HttpConnection.POST);
                connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

                os = connection.openOutputStream();
                byte data[] =("j_username=appliusername&j_password=applipassword").getBytes();
                os.write(data);
                os.flush();
                int respCode=connection.getResponseCode();

           if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
                is = connection.openInputStream();
                if (is != null ) {
                    int ch = -1;
                    while ((ch = is.read()) != -1) {

                    sb.append((char)ch);
                     }

               }
          }else {
              System.out.println("Error in opening HTTP Connection. Error#" + respCode);
           }              
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
             try {
                    if (is != null) {
                        is.close();
                        //is = null;
                    }
                     if (os != null) {
                        os.close();
                        //is = null;
                    }
                    if (connection != null) {
                        connection.close();
                       // connection = null;
                    }
              }catch (Exception e2) {
                    e2.printStackTrace();
            }
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用网址的权限部分并从

更改
"http://localhost:8080/myAppli/j_spring_security_check"  

"http://appliusername:applipassword@localhost:8080/myAppli/j_spring_security_check"

有关此问题的详情,请参阅http://en.wikipedia.org/wiki/URI_scheme#Examples