January 12, 2015

Exception “Received fatal alert: handshake_failure” while sending a request through java code or Error response: 401 java.io.IOException: Server returned HTTP response code: 401 for URL:

Issue:
1 .Exception “Received fatal alert: handshake_failure” while sending a request through java code or  
2 .Error orresponse: 401 java.io.IOException: Server returned HTTP response code: 401 for URL:
3. Exception in thread "main" javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
4.Same url is working successfully in browser, but it’s not working through java program.

Solutions for all above issues for this program, please try and post your comments.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
/**
 * A simple example that uses HttpClient to perform a GET using Basic
 * Authentication. Can be run standalone without parameters. 
 * You need to have JSSE on your classpath for JDK prior to 1.4
 * 
 * @author Myjavacafe
 */
 public class BasicHttpsAuthTest {
  
  /**
  * Constructor for BasicHttpsAuthTest.
  */
  public BasicHttpsAuthTest() {
   super();
  }
  public static void main(String[] args) throws Exception {
 String URL_SECURED_BY_BASIC_AUTHENTICATION = "https://<host>:<port&tt;/<etc...>";
    CredentialsProvider provider = new BasicCredentialsProvider();
    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(<userNaem>,<Passwrod> );
    provider.setCredentials(AuthScope.ANY, credentials);
    HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
   HttpResponse response = client.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION));
    System.out.println("Response Code :"+ response.getStatusLine().getStatusCode());
    int statusCode = response.getStatusLine().getStatusCode();
    if (statusCode != 200) {
  throw new RuntimeException("Failed : HTTP error code : "+ response.getStatusLine().getStatusCode());
    }
    BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
    String output;
    while ((output = br.readLine()) != null) {
     System.out.println("Response :" + output);
    }
    br.close();//If not mentioned - "Client connection failed" warnings from HornetQ in JBoss EAP
  }
}

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts