http帖子成功登录?

时间:2011-09-25 18:22:49

标签: android http authentication https http-post

我正在尝试登录到在post命令中需要3个参数的网站。 令牌,usr_name和usr_password。

令牌始终具有以下值“545616f1e29bc538843ec7aa908122b1e”。 我通过在登录页面上执行HttpGet并将其存储为字符串来获得此值。

如果我通过网址登录如下https://www.xxxxx.com/xxxx/restricted/form/formelement=0123?usr_name=myuser&usr_password=mypass&token=545616f1e29bc538843ec7aa908122b1e,则登录成功。

如何将a.m链接构建在一起,然后知道我成功登录? 感谢您的任何提示并帮助我。

我的代码:

try {
            String webPage = "https://xxxxxxxx.com/xx/Authenticationserv";
            String name = username; // user input through editbox
            String password1 = password; // user input through editbox

            String authString = name + ":" + password1 + ":" + token + "=" + value;
            System.out.println("auth string: " + authString);
            byte[] authEncBytes = Base64.encodeBytesToBytes(authString.getBytes());
            String authStringEnc = new String(authEncBytes);
            System.out.println("Base64 encoded auth string: " + authStringEnc);

            URL url = new URL(webPage);
            URLConnection urlConnection = url.openConnection();
            urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
            InputStream is = urlConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);

            int numCharsRead;
            char[] charArray = new char[1024];
            StringBuffer sb1 = new StringBuffer();
            while ((numCharsRead = isr.read(charArray)) > 0) {
                sb1.append(charArray, 0, numCharsRead);
            }
            String result = sb1.toString();

            System.out.println("/// BEGIN ///");
            System.out.println(result);
            System.out.println("/// END ///");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

1 个答案:

答案 0 :(得分:0)

实际上我认为您需要使用POST方法登录您的网站。几个星期前我遇到了同样的问题我已经这样做了:

    HttpClient httpclient;
    HttpPost httppost;
    ArrayList<NameValuePair> postParameters;
    httpclient = new DefaultHttpClient();
    httppost = new HttpPost("your login link");


    postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("username_hash", "fcd86e8cc9fc7596f102de7b2b922e80c6e6fac9"));
    postParameters.add(new BasicNameValuePair("password_hash", "b66936348bd0bd44fa44f5ca7dcceb909545e47f"));

    httppost.setEntity(new UrlEncodedFormEntity(postParameters));

    HttpResponse response = httpclient.execute(httppost);
    Log.w("Response ","Status line : "+ response.toString());

因此,您使用ArrayList设置邮政参数,如果您通过HttpResponse登录,则可以从服务器获得响应。还有一件事:我正在设置用户名和密码在代码中,因为它只是你的想法。如果你有任何问题随时可以问。

希望它有所帮助!