title: basic authentication with http post params android
body: <p>I am doing basic authentication by passing username and password and then using <code>BasicNameValuePair</code> for sending post params to get the response from the service.</p>
  
  <p>My method:</p>
  
  <pre><code>public StringBuilder callServiceHttpPost(String userName, String password, String type)
      {
  
          // Create a new HttpClient and Post Header
          HttpClient httpclient = new DefaultHttpClient();
          HttpPost httppost = new HttpPost(WEBSERVICE + type);
  
  
  
          HttpResponse response = null;
  
          StringBuilder total = new StringBuilder();
  
          try {
  
              URL url = new URL(WEBSERVICE + type);
  
              /*String base64EncodedCredentials = Base64.encodeToString((userName
                      + ":" + password).getBytes(), Base64.URL_SAFE
                      | Base64.NO_WRAP);*/
  
              String base64EncodedCredentials = "Basic " + Base64.encodeToString(
                      (userName + ":" + password).getBytes(),
                      Base64.NO_WRAP);
  
  
              httppost.setHeader("Authorization", base64EncodedCredentials);
  
              // Add your data
              List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(4);
              nameValuePairs.add(new BasicNameValuePair("day", Integer.toString(2)));
              nameValuePairs.add(new BasicNameValuePair("emailId", "usertest@gmail.com"));
              nameValuePairs.add(new BasicNameValuePair("month", Integer.toString(5)));
              nameValuePairs.add(new BasicNameValuePair("year", Integer.toString(2013)));
  
              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  
              // Execute HTTP Post Request
              response = httpclient.execute(httppost);
  
          } catch (ClientProtocolException e) {
              // TODO Auto-generated catch block
          } catch (IOException e) {
              // TODO Auto-generated catch block
          }
  
          HttpEntity entity = response.getEntity();
  
          if (entity != null) {
              try {
                  InputStream instream = entity.getContent();
  
                  String line = "";
  
  
                  // Wrap a BufferedReader around the InputStream
                  BufferedReader rd = new BufferedReader(new InputStreamReader(instream));
  
                  // Read response until the end
                  while ((line = rd.readLine()) != null) { 
                      total.append(line); 
                  }
  
              } catch (IllegalStateException e) {
  
                  e.printStackTrace();
              } catch (IOException e) {
  
                  e.printStackTrace();
              }
          }
  
  
          return total;
      }
  </code></pre>
  
  <p>But I am getting this in total:</p>
  
  <pre><code> &lt;html&gt;&lt;head&gt;   &lt;title&gt;Status page&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;h3&gt;Invalid json representation of content&lt;/h3&gt;&lt;p&gt;You can get technical details &lt;a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.1"&gt;here&lt;/a&gt;.&lt;br&gt;Please continue your visit at our &lt;a href="/"&gt;home page&lt;/a&gt;.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>