1

我有一个使用 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();
            }
        }
4

1 回答 1

0

您可以尝试使用 URL 的授权部分并从

"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

于 2012-10-17T11:43:48.560 回答