Android http发布并获取

时间:2012-01-20 01:51:49

标签: android http post get

我正在尝试登录openSIS学生信息系统。问题是,当我尝试获取Modules.php?modname = Students / Student.php页面时,它返回Modules.php。 我是否需要为帖子设置cookie并获取?

public void loginClicked(View v) throws IOException {
    if (v.getId() == R.id.loginBtn) {
        String siteResponse;
        siteResponse = postData("http://site.com/sis/index.php");

        if (siteResponse.contains("Note:")) {
            Toast.makeText(getBaseContext(), "Wrong username or password",
                    Toast.LENGTH_LONG).show();

            // setContentView(R.layout.mpaste);
            EditText edit = (EditText) findViewById(R.id.resultText);
            edit.setText(siteResponse);
        } else {
            Toast.makeText(getBaseContext(), "Logged in! ",
                    Toast.LENGTH_LONG).show();



        }

        String getHttpResponse = getInputStreamFromUrl("http://site/sis/Modules.php?modname=Students/Student.php");

        EditText edit = (EditText) findViewById(R.id.resultText);
        edit.setText(getHttpResponse);

    }

}

private StringBuilder getResponse(InputStream stream) {
    String line = "";
    StringBuilder result = new StringBuilder();
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(stream));
    try {
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

private String postData(String stream) throws ClientProtocolException,
        IOException {
    String siteResponse;

    HttpPost post = new HttpPost(stream);
    BasicHttpContext mHttpContext = new BasicHttpContext();
    CookieStore mCookieStore = new BasicCookieStore();
    mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);

    EditText username = (EditText) findViewById(R.id.editUsername);
    EditText password = (EditText) findViewById(R.id.editPassword);

    String enteredUsername;
    String enteredPassword;
    enteredUsername = username.getText().toString();
    enteredPassword = password.getText().toString();

    List<NameValuePair> userData = new ArrayList<NameValuePair>(2);
    userData.add(new BasicNameValuePair("USERNAME", enteredUsername));
    userData.add(new BasicNameValuePair("PASSWORD", enteredPassword));

    post.setEntity(new UrlEncodedFormEntity(userData));
    HttpResponse response = client.execute(post, mHttpContext);

    siteResponse = getResponse(response.getEntity().getContent())
            .toString();

    return siteResponse;

}

private String getInputStreamFromUrl(String url) {
    String content = null;
    try {

        BasicHttpContext mHttpContext = new BasicHttpContext();
        CookieStore mCookieStore = new BasicCookieStore();
        mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore);
        HttpResponse response = client.execute(new HttpGet(url),
                mHttpContext);
        content = getResponse(response.getEntity().getContent()).toString();
    } catch (Exception e) {

    }
    return content;
}

}

0 个答案:

没有答案