• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Understanding HTTP POST response

kbless7

Lurker
I'm trying to use HTTP POST to submit user information on a University login page. To do so I run the postData() method below. The code posts the credentials and reads the response. Below the code I have shown what the response status line and response entity show. (I edited the username and password for security ;) ).

It appears to execute since I get a response from the website, but I don't know how to interpret what it says or what I need to do to have a successful login. The website in question has a username field (id = user), password field (id = pass), and login button (id = submit). It is a secure website (https), but for now I'm not doing a SSL connection as I'm just trying to get this code working.

The immediate questions I have are: is it possible I need the SSL connection to make it work, and is Java an issue as mentioned in the response?

A little more background... This is done in a service, and it's the only task. If Java is the issue, how would I enable it without a webview? Can I somehow "click" the button to see where it takes me?

Code:
public void postData() {
        // Create a new HttpClient and Post Header
    	HttpParams params = new BasicHttpParams();
    	params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpClient httpclient = new DefaultHttpClient(params);
        HttpPost httppost = new HttpPost("https://wiscmail.wisc.edu/login/");
        TextView info = (TextView) findViewById(R.id.info);
        EditText user = (EditText) findViewById(R.id.user);
        EditText pass = (EditText) findViewById(R.id.pass);

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("user", user.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("pass", pass.getText().toString()));
            //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
            httppost.setHeader("Accept", "application/x-www-form-urlencoded");
            
            AbstractHttpEntity ent=new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
            ent.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
            ent.setContentEncoding("UTF-8");
            httppost.setEntity(ent);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            Log.v(TAG, response.getStatusLine().toString());
            info.setText(response.getStatusLine().toString());
            
            HttpEntity responseEntity = response.getEntity();
            Log.v(TAG, responseEntity.toString());
            
            String response_string = inputStreamToString(response.getEntity().getContent()).toString();
            info.setText(response_string);
            Log.v(TAG, response_string);
            
        } catch (ClientProtocolException e) {
        	Log.v(TAG, "client protocol exception");
        	info.setText("client protocol exception");
        } catch (IOException e) {
        	Log.v(TAG, "IO exception");
        	info.setText("IO exception");
        }
    }
     
    // Fast Implementation
    private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        // Read response until the end
        try {
			while ((line = rd.readLine()) != null) { 
			    total.append(line); 
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
        
        // Return full string
        return total;
    }

response.getStatusLine
Code:
HTTP/1.1 200 OK

response.getEntity().getContent()
Code:
<html><head></head><body onLoad="document.relay.submit()">
<form method=post action="https://login.wisc.edu/?appurl=wiscmail.wisc.edu/login"
name=relay><input type=hidden name=pubcookie_g_req 
value="b25lPXdpc2NtYWlsLndpc2MuZWR1JnR3bz1XaXNjTWFpbCtMb2dpbiZ0aHJlZT0xJmZvdXI9YTUmZml2ZT1QT1NUJnNpeD13aXNjbWFpbC53aXNjLmVkdSZzZXZlbj1MMnh2WjJsdUx3PT0mZWlnaHQ9Jmhvc3RuYW1lPXdpc2NtYWlsLndpc2MuZWR1Jm5pbmU9MSZmaWxlPSZyZWZlcmVyPShudWxsKSZzZXNzX3JlPTAmcHJlX3Nlc3NfdG9rPTM5NjgzODc5MSZmbGFnPTA=">
<input type=hidden name=post_stuff value="user=username&pass=password">
<input type=hidden name=relay_url value="https://wiscmail.wisc.edu/PubCookie.reply">
<noscript><p align=center>You do not have Javascript turned on,   please click the
button to continue.<p align=center><input type=submit name=go value=Continue>
</noscript></form></html>
 
Back
Top Bottom