Android logon to any asp website
EDITED: Java httpPost into .asp form
I am making an application that will display some information from the website, which is .asp, to access some of the data I must login. How can I post my credentials(login, password) to the server and get the page in return. I also think that there a lot of redirects on the way, when I login. I have no idea about asp. What I am doing, I am just posting the username, password to the server and I can retrieve the source code of that page with my posted information in the right place. But what do I do next? I thought That there could be one code snippet that can be used with any .asp website engine.
EDIT: Ok, page code is received Authentication form has been of IIS type.
when specifuing nameValuePairs use html input (name, value) pairs.
Login form:
<form method="post" id="form1" name="form1" action="">
<input id="txtUser" name="txtUser" type="text" size="13" value="" />
<input id="txtPassword" name="txtPassword" type="password" size="13" value="" />
<input id="BLogin" name="BLogin" type="submit" value="Log in" />
<input type="checkbox" id="chkSave" name="chkSave"/>
</form>
some JAVA:
HttpPost httppost = new HttpPost("WEBSITE WITH LOGI开发者_如何学JAVAN PAGE (Ex: www.qwerty.asd/login.asp)");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("txtUser", "YOUR LOGIN NAME"));
nameValuePairs.add(new BasicNameValuePair("txtPassword", "YOUR PASSWORD"));
nameValuePairs.add(new BasicNameValuePair("BLogin", "Log in"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
entity = response.getEntity();
System.out.println("Login form get: " + response.getStatusLine());
if (entity != null) {
entity.consumeContent();
}
System.out.println("Post logon cookies:");
cookies = httpclient.getCookieStore().getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i));
// httpclient.getCookieStore().addCookie(cookies.get(i));
}
}
Getting headers:
Header[] headers = response.getAllHeaders();
for(int i=0; i<headers.length; i++){
//System.out.println(headers[i]);
Header h = headers[i];
System.out.println(h.getName());
System.out.println(h.getValue());
}
Proceed to any other page within same service:
HttpPost request2 = new HttpPost(
"www.qwerty.asd/information.asp");
response = httpclient.execute(request2);
String responseBody2 = EntityUtils.toString(response.getEntity());
//System.out.println(responseBody2);
if (entity != null) {
entity.consumeContent();
}
For Android Webview to display the web page:
WebView.loadData(responseBody2, "text/html", "utf-8");
.asp has nothing to do with Android directly.
You are looking to make a standard HTTP REST request using a WebView.
Also, here is a page that includes many common tasks in Android as well as links to many tutorials, articles and code samples : http://developer.android.com/resources/faq/commontasks.html
EDIT: Many might say use a standard alert dialog with a login button BUT I do not believe in the alert dialogs because they have issues. To see an example, create a simple dialog that does nothing and change the screen orientation... it might seem fine but if you look at the LogCat you will see it leaks the activity.
Therefore, I recommend creating a regular Activity and using the android:theme="@android:style/Theme.Dialog" in the activity tag in your manifest to make the activity look like a dialog.
Boris, Give you problem, please try following.
- From the browser, check all the URLs and status codes that browser receives after you login and returns the page you need. For this you will need some browser plugin like YSlow or FireBug (http://getfirebug.com/). Install any of these plugin and look at the request/response.
- Now please have brief look at apache's http-client library. Check out the URL http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient .
- This library is very powerful and it is capable of achieving anything a browser can achieve (with respect to interaction with http servers).
- Now from #1 above you have the pattern of the request/response and http-client lib which can achieve this programmatically.
精彩评论