title: Programmatically logging in from WebView
body: <p>In my Android application I'm using a web view  to access some web mapping data provided by a server. The server requires some HTTP form based authentication to allow access to those data. Due to the fact that the site doesn't have a mobile version, displaying the login page (or any other pages) looks pretty bad . Unfortunately the site is hardly into my reach so I've thought of the following approach: </p>
  
  <ul>
  <li>use a native user interface to collect the username and password</li>
  <li>thought a Http post send those information to the server</li>
  <li>after the response is received get the cookies the server is sending</li>
  <li>set the cookies to the the web view</li>
  <li>try to finally access the desired data</li>
  </ul>
  
  <p>For now I'm just trying to pass the login phase.</p>
  
  <p>Is this a viable solution , or is just plain wrong and I should try something else ?</p>
  
  <p>For completeness I post the code below </p>
  
  <p>A. The authentication part</p>
  
  <pre><code> private String authenticate()  throws Exception
      {
          // Create a new HttpClient and Post Header
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost("http://mySite/login_form");
  
             HttpResponse response = null;
             BufferedReader in = null;
             String resultContent = null;
  
             try
             {
                 // Add data
                 List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
                 nameValuePairs.add(new BasicNameValuePair("came_from", ""));
                 nameValuePairs.add(new BasicNameValuePair("form.submitted", "1"));
                 nameValuePairs.add(new BasicNameValuePair("js_enabled", "0"));
                 nameValuePairs.add(new BasicNameValuePair("cookies_enabled", ""));
                 nameValuePairs.add(new BasicNameValuePair("login_name", ""));
                 nameValuePairs.add(new BasicNameValuePair("pwd_empty", "0"));
                 nameValuePairs.add(new BasicNameValuePair("name", "username"));
                 nameValuePairs.add(new BasicNameValuePair("password", "password"));
  
                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
                  // Create a local instance of cookie store
                  CookieStore cookieStore = new BasicCookieStore();
                  // Create local HTTP context
                  HttpContext localContext = new BasicHttpContext();
                  // Bind custom cookie store to the local context
                  localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
                 // Execute HTTP Post Request
                 response = httpclient.execute(httppost,localContext);
  
                 in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                 StringBuffer sb = new StringBuffer("");
                 String line = "";
                 String NL = System.getProperty("line.separator");
                 while ((line = in.readLine()) != null)
                 {
                   sb.append(line + NL);
                 }
                 in.close();
                 resultContent = sb.toString();
                 Log.i("mytag","result :"+resultContent);
  
                 cookies = new java.util.ArrayList();
                 cookies = cookieStore.getCookies();
  
             }
             catch (ClientProtocolException e)
             {
                 Log.i("mytag","Client protocol exception");
             }
             catch (IOException e)
             {
                 Log.i("mytag","IOException");
             }
             catch(Exception e)
             {
                 Log.i("mytag","Exception");
                 Log.i("mytag",e.toString());
             }
  
  
          return resultContent;
  
      }
  </code></pre>
  
  <p>B. Setting the cookies and loading the desired page</p>
  
  <pre><code>private void init()
  {
              CookieSyncManager.createInstance(this);
              CookieManager cookieMan= CookieManager.getInstance();
              cookieMan.setAcceptCookie(true);
              cookies = StartupActivity.listAfter;
  
              if(cookies != null)
              {
                  for (int i = 0; i&lt;cookies.size(); i++)
                  {
                      Cookie cookie = cookies.get(i);
                      cookieMan.setCookie("cookie.getDomain()",cookie.getValue());
                  }
              }
              CookieSyncManager.getInstance().sync();
  
              webView = (WebView)findViewById(R.id.web_view);
              webView.getSettings().setJavaScriptEnabled(true);
              webView.getSettings().setBuiltInZoomControls(true);
              webView.setWebViewClient(new HelloWebViewClient());
  
  }
  
       protected void onResume()
       {
              super.onResume();    
  
              // test if the we logged in
              webView.loadUrl("mySite/myDesiredFeature");
  
       }
  </code></pre>
  
  <p>The results of loading that page is that the login_page form is displayed</p>