HttpRetryException in Java

java.net.HttpRetryException

This thrown to indicate that an HTTP request needs to be retried but cannot be retried automatically, due to streaming mode being enabled.

Declaration

public class HttpRetryException extends IOException {
    @java.io.Serial
    private static final long serialVersionUID =
-9186022286469111381L;

    private int responseCode;
    private String location;
   
    public HttpRetryException(String detail, int code) {
        super(detail);
        responseCode = code;
    }

    public HttpRetryException(String detail,
int code, String location) {
        super (detail);
        responseCode = code;
        this.location = location;
    }

    public int responseCode() {
        return responseCode;
    }

    public String getReason() {
        return super.getMessage();
    }

   
    public String getLocation() {
        return location;
    }
}

Methods

1. HttpRetryException(String detail, int code)

java.net.HttpRetryException.HttpRetryException(String detail, int code)

This method takes two arguments. This method constructs a new HttpRetryException from the specified response code and an exception detail message.

Parameters: Two parameters are required for this method.

detail: the detail message.

code: the HTTP response code from the server.

Exceptions: NA

Approach

Java

package com.HttpRetryException;

import java.net.HttpRetryException;

public class HttpRetryException1 {
    public static void main(String[] args) {

        String detail = "Error occurred";
        int code = 419;
        HttpRetryException httpRetryException =
new HttpRetryException(detail, code);

        System.out.println(httpRetryException);
    }
}

Output:

java.net.HttpRetryException: Error occurred


2. HttpRetryException(String detail, int code, String location)

java.net.HttpRetryException.HttpRetryException(String detail, int code, String location)

This method takes three arguments. This method constructs a new HttpRetryException with a detailed message response code and the contents of the Location response header field.

Parameters: Three parameters are required for this method.

detail: the detail message.

code: the HTTP response code from the server.

location: the URL to be redirected to.

Exceptions: NA

Approach

Java

package com.HttpRetryException;

import java.net.HttpRetryException;

public class HttpRetryException2 {
    public static void main(String[] args) {

        String detail = "Error occurred";
        int code = 419;
        String location = "main";
        HttpRetryException httpRetryException =
new HttpRetryException(detail, code, location);

        System.out.println(httpRetryException);
    }
}

Output:

java.net.HttpRetryException: Error occurred


3. getLocation()

String java.net.HttpRetryException.getLocation()

This method returns the value of the Location header field if the error resulted from redirection.

Parameters: NA

Returns: The location string.

Exceptions: NA

Approach

Java

package com.HttpRetryException;

import java.net.HttpRetryException;

public class HttpRetryExceptiongetLocation {
    public static void main(String[] args) {

        String detail = "Error occurred";
        int code = 419;
        String location = "main";
        HttpRetryException httpRetryException =
new HttpRetryException(detail, code, location);

        System.out.println(httpRetryException.getLocation());
    }
}

Output:

main


4. HttpRetryException.getReason()

String java.net.HttpRetryException.getReason()

This method returns a string explaining why the http request could not be retried.

Parameters: NA

Returns: The reason string.

Exceptions: NA

Approach

Java

package com.HttpRetryException;

import java.net.HttpRetryException;

public class HttpRetryExceptiongetReason {
    public static void main(String[] args) {

        String detail = "Error occurred";
        int code = 419;
        String location = "main";
        HttpRetryException httpRetryException =
new HttpRetryException(detail, code, location);

        System.out.println(httpRetryException.getReason());
    }
}

Output:

Error occurred


5. HttpRetryException.responseCode()

int java.net.HttpRetryException.responseCode()

This method returns the http response code.

Parameters: NA

Returns: The http response code.

Exceptions: NA

Approach

Java

package com.HttpRetryException;

import java.net.HttpRetryException;

public class HttpRetryExceptionresponseCode {
    public static void main(String[] args) {

        String detail = "Error occured";
        int code = 419;
        String location = "main";
        HttpRetryException httpRetryException =
new HttpRetryException(detail, code, location);

        System.out.println(httpRetryException.responseCode());
    }
}

Output:

419

No comments:

Post a Comment