i have a problem logging in to a website made in asp... Here is the code i am using:
public class MainActivity extends Activity {
Button bottone;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottone = (Button) findViewById(R.id.button1);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
bottone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// starting new Async Task
try {
login();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public static void login() throws IOException
{
String loginurl = "https://www.blurum.it/Web/", user, pass;
user = "tester55";
pass = "provapassword321";
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(loginurl);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$Username", user));
nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$Password", pass));
nameValuePairs.add(new BasicNameValuePair("remember", "on"));
nameValuePairs.add(new BasicNameValuePair("ctrl_LoginControl$loginView$c01$RPLogin$LoginView$LoginButton", "Accedi"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
Log.d("logggd", "response stat code " + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() < 400) {
String cookie = response.getFirstHeader("Cookie")
.getValue();
Log.d("logggdc", "cookie: " + cookie);
// get the contacts page
HttpGet getContacts = new HttpGet("https://www.blurum.it/Web/default.aspx");
getContacts.setHeader("Cookie", cookie);
response = httpClient.execute(getContacts);
InputStream ins = response.getEntity().getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(
ins));
String inputLine;
while ((inputLine = in.readLine()) != null) {
Log.d("logggd1", " " + inputLine);
}
in.close();
} else {
Log.d("logggd2", "Response error: "
+ response.getStatusLine().getStatusCode());
}
}
}
But my application crash, so i can't find the error in the code... Here is the logcat:
11-12 20:05:37.678: W/dalvikvm(1806): threadid=1: thread exiting with uncaught exception (group=0xb4177180)
11-12 20:05:37.688: E/AndroidRuntime(1806): FATAL EXCEPTION: main
11-12 20:05:37.688: E/AndroidRuntime(1806): java.lang.NullPointerException
11-12 20:05:37.688: E/AndroidRuntime(1806): at com.example.testlogin.MainActivity.login(MainActivity.java:93)
11-12 20:05:37.688: E/AndroidRuntime(1806): at com.example.testlogin.MainActivity$1.onClick(MainActivity.java:53)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.view.View.performClick(View.java:3511)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.view.View$PerformClick.run(View.java:14105)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.os.Handler.handleCallback(Handler.java:605)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.os.Handler.dispatchMessage(Handler.java:92)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.os.Looper.loop(Looper.java:137)
11-12 20:05:37.688: E/AndroidRuntime(1806): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-12 20:05:37.688: E/AndroidRuntime(1806): at java.lang.reflect.Method.invokeNative(Native Method)
11-12 20:05:37.688: E/AndroidRuntime(1806): at java.lang.reflect.Method.invoke(Method.java:511)
11-12 20:05:37.688: E/AndroidRuntime(1806): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-12 20:05:37.688: E/AndroidRuntime(1806): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-12 20:05:37.688: E/AndroidRuntime(1806): at dalvik.system.NativeStart.main(Native Method)
11-12 20:05:38.218: I/dalvikvm(1806): threadid=3: reacting to signal 3
11-12 20:05:38.228: I/dalvikvm(1806): Wrote stack traces to '/data/anr/traces.txt'
I extracted the posted data to the server in firefox with Tamper Data, here is a screen, the login i think is divided in 2 post, here are the screen: http://i.stack.imgur.com/GZiZ0.png http://i.stack.imgur.com/FKH3P.png
I am not sure if i have to send the first page or the second post data, can be this the error? thanks.
It seems response.getFirstHeader("Cookie")
is returning null
.
As per javadoc
Returns the first header with a specified name of this message. Header values are ignored. If there is more than one matching header in the message the first element of getHeaders(String) is returned. If there is no matching header in the message null is returned
Solution:
Do a null
check before calling getValue();
Example:
String cookie = null;
if(response.getFirstHeader("Cookie") != null)
{
cookie = response.getFirstHeader("Cookie")
.getValue();
}
You can't call the below line more than once in your code block;
response.getStatusLine()
You are calling it more than once in your lines that look like this;
Log.d("logggd", "response stat code " + response.getStatusLine().getStatusCode());
And then you're calling it again on this line;
if (response.getStatusLine().getStatusCode() < 400) {
Comment out the first calling of response.getStatusLine()
- that's where you're trying to log the status code. So your code should look like this;
//Log.d("logggd", "response stat code " + response.getStatusLine().getStatusCode());
You also want to try and do this somewhere there in your method called login()
;
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
Log.d("logggd", "response stat code " + statusLine.getStatusCode());
if (statusLine.getStatusCode() < 400) {
String cookie = null;
if(response.getFirstHeader("Cookie") != null){
cookie = response.getFirstHeader("Cookie").getValue();
}
Log.d("logggdc", "cookie: " + cookie);
//...etc....etc...etc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.