1. Download our Official Android App: Forums for Android!

Apps Understanding HTTP POST response

Discussion in 'Android Development' started by kbless7, Jan 13, 2012.

  1. kbless7

    kbless7 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Jan 13, 2012

    Jan 13, 2012
    1
    0
    5
    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 (Text):
    1. public void postData() {
    2.         // Create a new HttpClient and Post Header
    3.         HttpParams params = new BasicHttpParams();
    4.         params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    5.         HttpClient httpclient = new DefaultHttpClient(params);
    6.         HttpPost httppost = new HttpPost("https://wiscmail.wisc.edu/login/");
    7.         TextView info = (TextView) findViewById(R.id.info);
    8.         EditText user = (EditText) findViewById(R.id.user);
    9.         EditText pass = (EditText) findViewById(R.id.pass);
    10.  
    11.         try {
    12.             // Add your data
    13.             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    14.             nameValuePairs.add(new BasicNameValuePair("user", user.getText().toString()));
    15.             nameValuePairs.add(new BasicNameValuePair("pass", pass.getText().toString()));
    16.             //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    17.             httppost.setHeader("Content-type", "application/x-www-form-urlencoded");
    18.             httppost.setHeader("Accept", "application/x-www-form-urlencoded");
    19.            
    20.             AbstractHttpEntity ent=new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8);
    21.             ent.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
    22.             ent.setContentEncoding("UTF-8");
    23.             httppost.setEntity(ent);
    24.  
    25.             // Execute HTTP Post Request
    26.             HttpResponse response = httpclient.execute(httppost);
    27.             Log.v(TAG, response.getStatusLine().toString());
    28.             info.setText(response.getStatusLine().toString());
    29.            
    30.             HttpEntity responseEntity = response.getEntity();
    31.             Log.v(TAG, responseEntity.toString());
    32.            
    33.             String response_string = inputStreamToString(response.getEntity().getContent()).toString();
    34.             info.setText(response_string);
    35.             Log.v(TAG, response_string);
    36.            
    37.         } catch (ClientProtocolException e) {
    38.             Log.v(TAG, "client protocol exception");
    39.             info.setText("client protocol exception");
    40.         } catch (IOException e) {
    41.             Log.v(TAG, "IO exception");
    42.             info.setText("IO exception");
    43.         }
    44.     }
    45.      
    46.     // Fast Implementation
    47.     private StringBuilder inputStreamToString(InputStream is) {
    48.         String line = "";
    49.         StringBuilder total = new StringBuilder();
    50.        
    51.         // Wrap a BufferedReader around the InputStream
    52.         BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    53.  
    54.         // Read response until the end
    55.         try {
    56.             while ((line = rd.readLine()) != null) {
    57.                 total.append(line);
    58.             }
    59.         } catch (IOException e) {
    60.             e.printStackTrace();
    61.         }
    62.        
    63.         // Return full string
    64.         return total;
    65.     }
    response.getStatusLine
    Code (Text):
    1. HTTP/1.1 200 OK
    response.getEntity().getContent()
    Code (Text):
    1. <html><head></head><body onLoad="document.relay.submit()">
    2. <form method=post action="https://login.wisc.edu/?appurl=wiscmail.wisc.edu/login"
    3. name=relay><input type=hidden name=pubcookie_g_req
    4. value="b25lPXdpc2NtYWlsLndpc2MuZWR1JnR3bz1XaXNjTWFpbCtMb2dpbiZ0aHJlZT0xJmZvdXI9YTUmZml2ZT1QT1NUJnNpeD13aXNjbWFpbC53aXNjLmVkdSZzZXZlbj1MMnh2WjJsdUx3PT0mZWlnaHQ9Jmhvc3RuYW1lPXdpc2NtYWlsLndpc2MuZWR1Jm5pbmU9MSZmaWxlPSZyZWZlcmVyPShudWxsKSZzZXNzX3JlPTAmcHJlX3Nlc3NfdG9rPTM5NjgzODc5MSZmbGFnPTA=">
    5. <input type=hidden name=post_stuff value="user=username&pass=password">
    6. <input type=hidden name=relay_url value="https://wiscmail.wisc.edu/PubCookie.reply">
    7. <noscript><p align=center>You do not have Javascript turned on,   please click the
    8. button to continue.<p align=center><input type=submit name=go value=Continue>
    9. </noscript></form></html>
     

    Advertisement

Share This Page

Loading...