title: Why my Http client making 2 requests when I specify credentials?
body: <p>I created RESTful webservice (WCF) where I check credentials on each request. One of my clients is Android app and everything seems to be great on server side. I get request and if it's got proper header - I process it, etc..</p>
  
  <p>Now I created client app that uses this service. This is how I do GET:</p>
  
  <pre><code>// Create the web request  
              var request = WebRequest.Create(Context.ServiceURL + uri) as HttpWebRequest;
  
              if (request != null)
              {
                  request.ContentType = "application/json";
  
                  // Add authentication to request  
                  request.Credentials = new NetworkCredential(Context.UserName, Context.Password);
  
                  // Get response  
                  using (var response = request.GetResponse() as HttpWebResponse)
                  {
                      // Get the response stream  
                      if (response != null)
                      {
                          var reader = new StreamReader(response.GetResponseStream());
  
                          // Console application output
                          var s = reader.ReadToEnd();
  
                          var serializer = new JavaScriptSerializer();
                          var returnValue = (T)serializer.Deserialize(s, typeof(T));
  
                          return returnValue;
                      }
                  }
              }
  </code></pre>
  
  <p>So, this code get's my resource and deserializes it. As you see - I'm passing credentials in my call.</p>
  
  <p>Then when debugging on server-side I noticed that I get 2 requests every time - one without authentication header and then server sends back response and second request comes bach with credentials. I think it's bad for my server - I'd rather don't make any roundtrips. How should I change client so it doesn't happen? See screenshot of Fiddler</p>
  
  <p><img src="https://i.stack.imgur.com/UBDHS.png" alt="First BAD request"></p>
  
  <p><img src="https://i.stack.imgur.com/aakmf.png" alt="Second GOOD request"></p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>This is JAVA code I use from Android - it doesn't do double-call:</p>
  
  <pre><code>MyHttpResponse response = new MyHttpResponse();
          HttpClient client = mMyApplication.getHttpClient();
  
          try
          {
              HttpGet request = new HttpGet(serviceURL + url);
              request.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
              request.addHeader("Authorization", "Basic " + Preferences.getAuthorizationTicket(mContext));
  
              ResponseHandler&lt;String&gt; handler = new BasicResponseHandler();
              response.Body = client.execute(request, handler);
              response.Code = HttpURLConnection.HTTP_OK;
              response.Message = "OK";
          }
          catch (HttpResponseException e)
          {
              response.Code = e.getStatusCode();
              response.Message = e.getMessage();
  
              LogData.InsertError(mContext, e);
          }
  </code></pre>