使用POST方法进行身份验证

时间:2010-11-10 07:44:11

标签: java java-me

我正在尝试使用POST方法验证登录。但是,我收到了IOException并且Stream关闭了。

这是我的代码的一部分。

                String content = "";
                boolean keepGoing = true;

                int status = -1;           

                String _AuthenticationHash = getAuthData("username","password");                  

                // Open the connection and extract the data.
                try 
                {                        
                 HttpConnection httpConn = null;
                 httpConn = (HttpConnection)Connector.open(getUrl());            


                 // Setup HTTP Request to POST
                 httpConn.setRequestMethod(HttpConnection.POST);                    
                    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                    httpConn.setRequestProperty("Accept", "text/plain");  

                    httpConn.setRequestProperty(HttpProtocolConstants.HEADER_AUTHORIZATION, " Basic " + _AuthenticationHash);
                  //  httpConn.setRequestProperty("name", usernamelogin);
                    //httpConn.setRequestProperty("password", passwordlogin);
                    tempCookie = httpConn.getHeaderField("Set-Cookie");                  


                    System.out.println(tempCookie);


                    while(keepGoing)
                    {

                    status = httpConn.getResponseCode();            
                    //EventLogger.logEvent([doConnect], "Response Code:", status);

                    if (status == HttpConnection.HTTP_OK)
                    {
                        //Connection is 200 OK.
                        //Download and process data.

                        keepGoing = false;                                                 
                      content = "Validate response: Connected. \nresponse code = " + status;

                     // StringBuffer out = new StringBuffer();
            //InputStream in = httpConn.openInputStream();
            //byte[] b = new byte[4096];
            //for (int n; (n = in.read(b)) != -1;) {
             //out.append(new String(b, 0, n));
            //}
            //String test = out.toString();


                    }  

                    httpConn.close();                    
                }
                }
                catch (IOCancelledException e) 
                {       
                    System.out.println(e.toString());                        
                    return;
                }
                catch (IOException e) 
                {       
                    errorDialog("Unable to Connect \n" + e.toString());   

                    return;
                }                

                // Make sure status thread doesn't overwrite our content
                stopStatusThread();
                updateContent(content);                        

                // We're finished with the operation so reset
                // the start state.
                _fetchStarted = false;                    
            }
        }

        private String getAuthData(String userName, String userPassWord) { 

         userName = "admin";
         userPassWord = "1234";

      // Update authentication hash from current userName & userPassWord.
      byte[] toEncode = (userName + ":" + userPassWord).getBytes();
      ByteArrayOutputStream newHash = new ByteArrayOutputStream(toEncode.length);
      Base64OutputStream base64OutputStream = new Base64OutputStream(newHash);

      try {
       base64OutputStream.write(toEncode, 0, toEncode.length);
       base64OutputStream.flush();
       base64OutputStream.close();
      } catch (IOException ioe) {
       // Null out hash.
       newHash = null;
      }

      // Set authentication hash, clear password.
      return newHash.toString();
     }

0 个答案:

没有答案