{"id":71,"date":"2011-02-03T19:34:22","date_gmt":"2011-02-03T18:34:22","guid":{"rendered":"http:\/\/www.mustdev.com\/?p=71"},"modified":"2021-03-29T11:56:05","modified_gmt":"2021-03-29T10:56:05","slug":"java-defaulthttpclient-with-basic-auth","status":"publish","type":"post","link":"https:\/\/www.mustdev.com\/en\/java-defaulthttpclient-with-basic-auth\/","title":{"rendered":"Java DefaultHttpClient with basic auth"},"content":{"rendered":"<div>\n<p>One common task that we have to face during the development is to make an http client.<br \/>\nSometimes we don&#8217;t make only http request via get or post method, but we also have to authenticate request to retrive data.<br \/>\nOne type of common authentication that we can run into is Apache Basic Authentication. This authentication is very simple because it only requires an username and password pair sent in plain text.<br \/>\nDefaultHttpClient is a Java class that allows us to make requests via http get\/post with a basic auth. This class is not included in standard Java SDK, but is part of a library package called Apache HttpComponents.<\/p>\n<p><!--more--><img title=\"More...\" src=\"http:\/\/www.mustdev.com\/wp-includes\/js\/tinymce\/plugins\/wordpress\/img\/trans.gif\" alt=\"\">We can get more informations about this package here:<br \/>\n<a href=\"http:\/\/httpd.apache.org\/docs\/2.0\/howto\/auth.html\">httpd.apache.org\/docs\/2.0\/howto\/auth.html<\/a>.<\/p>\n<p>Below there is an usage example.<\/p>\n<pre><code>\nimport java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.InputStreamReader;\nimport java.net.URI;\nimport java.net.URISyntaxException;\n\nimport org.apache.http.HttpResponse;\nimport org.apache.http.auth.AuthScope;\nimport org.apache.http.auth.UsernamePasswordCredentials;\nimport org.apache.http.client.ClientProtocolException;\nimport org.apache.http.client.methods.HttpGet;\nimport org.apache.http.conn.ConnectTimeoutException;\nimport org.apache.http.impl.client.DefaultHttpClient;\nimport org.apache.http.params.CoreConnectionPNames;\n\npublic class HttpAuthRequest\n{\n\tpublic static void main(String[] args)\n\t{\n\t\tDefaultHttpClient client = null;\n\n\t\ttry\n\t\t{\n\t\t\t\/\/ Set url\n\t\t\tURI uri = new URI(\"http:\/\/yourdomain.com\/yourpage?yourparamname=yourparamvalue\");\n\n\t\t\tclient = new DefaultHttpClient();\n\n\t\t\tclient.getCredentialsProvider().setCredentials(\n\t\t\t\t\tnew AuthScope(uri.getHost(), uri.getPort(),AuthScope.ANY_SCHEME),\n\t\t\t\t\tnew UsernamePasswordCredentials(\"youruser\", \"yourpass\"));\n\n\t\t\t\/\/ Set timeout\n\t\t\tclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);\n\t\t\tHttpGet request = new HttpGet(uri);\n\n\t\t\tHttpResponse response = client.execute(request);\n\t\t\tif(response.getStatusLine().getStatusCode() == 200)\n\t\t\t{\n\t\t\t\tInputStream responseIS = response.getEntity().getContent();\n\t\t\t\tBufferedReader reader = new BufferedReader(new InputStreamReader(responseIS));\n\t\t\t\tString line = reader.readLine();\n\t\t\t\twhile (line != null)\n\t\t\t\t{\n\t\t\t\t\tSystem.out.println(line);\n\t\t\t\t\tline = reader.readLine();\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tSystem.out.println(\"Resource not available\");\n\t\t\t}\n\t\t}\n\t\tcatch (URISyntaxException e)\n\t\t{\n\t\t\tSystem.out.println(e.getMessage());\n\t\t}\n\t\tcatch (ClientProtocolException e)\n\t\t{\n\t\t\tSystem.out.println(e.getMessage());\n\t\t}\n\t\tcatch (ConnectTimeoutException e)\n\t\t{\n\t\t\tSystem.out.println(e.getMessage());\n\t\t}\n\t\tcatch (IOException e)\n\t\t{\n\t\t\tSystem.out.println(e.getMessage());\n\t\t}\n\t\tcatch (Exception e)\n\t\t{\n\t\t\tSystem.out.println(e.getMessage());\n\t\t}\n\t\tfinally\n\t\t{\n\t\t\tif ( client != null )\n\t\t\t{\n\t\t\t\tclient.getConnectionManager().shutdown();\n\t\t\t}\n\t\t}\n\t}\n}<\/code><\/pre>\n<p>For this example we must include this jar file:<\/p>\n<ul>\n<li><a title=\"Apache commons codec\" href=\"http:\/\/commons.apache.org\/codec\/download_codec.cgi\" target=\"_blank\" rel=\"noopener\">Apache common codec<\/a><\/li>\n<li><a title=\"Apache common logging\" href=\"http:\/\/commons.apache.org\/logging\/download_logging.cgi\" target=\"_blank\" rel=\"noopener\">Apache common logging<\/a><\/li>\n<li><a title=\"Apache HttpClient, HttpCore, Commons HttpClient\" href=\"http:\/\/hc.apache.org\/downloads.cgi\" target=\"_blank\" rel=\"noopener\">Apache HttpClient, HttpCore, Commons HttpClient<\/a><\/li>\n<\/ul>\n<p>That&#8217;s all!<br \/>\nHave fun!<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>One common task that we have to face during the development is to make an http client. Sometimes we don&#8217;t make only http request via get or post method, but we also have to authenticate request to retrive data. One type of common authentication that we can run into is Apache Basic Authentication. This authentication\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.mustdev.com\/en\/java-defaulthttpclient-with-basic-auth\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[76],"tags":[72,73,66],"_links":{"self":[{"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/posts\/71"}],"collection":[{"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/comments?post=71"}],"version-history":[{"count":11,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/posts\/71\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/posts\/71\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/media?parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/categories?post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mustdev.com\/en\/wp-json\/wp\/v2\/tags?post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}