title: Response code 401 when accesing rest based web services with correct credentials
body: <p>I am getting Unauthorized error when accessing restful web services. My sample program looks like this.</p>
  
  <pre><code>public static void main(String[] args){
          // Use apache commons-httpclient to create the request/response
          HttpClient client = new HttpClient();
          Credentials defaultcreds = new UsernamePasswordCredentials("aaa", "cdefg");
          client.getState().setCredentials(AuthScope.ANY, defaultcreds);
  
  
          GetMethod method = new GetMethod(
                  "http://localhost:8080/userService/usersByID/1234");
          try {
              client.executeMethod(method);
              InputStream in = method.getResponseBodyAsStream();
              // Use dom4j to parse the response and print nicely to the output stream
              BufferedReader reader = new BufferedReader(new InputStreamReader(in));
              StringBuilder out = new StringBuilder();
              String line;
              while ((line = reader.readLine()) != null) {
                  out.append(line);
              }
              System.out.println(out.toString());
          } catch (IOException e) {
              e.printStackTrace();
          } 
      }
  </code></pre>
  
  <p>My credentials are correct. My web services will consume Basic Http Authentication. </p>
  
  <p>I have doubt at scope of authentication.</p>
  
  <pre><code>client.getState().setCredentials(AuthScope.ANY, defaultcreds);
  </code></pre>
  
  <p>My credentials are correct.</p>
  
  <p>Can any one help to resolve this issue.</p>
  
  <p>Thanks.</p>