Response Status Code এবং Headers হ্যান্ডেল করা

Java Technologies - অ্যাপাচি এইচটিটিপি ক্লায়েন্ট (Apache HTTP Client) HTTP Client এর বেসিক ব্যবহার |
160
160

Apache HTTP Client দিয়ে HTTP Response থেকে স্ট্যাটাস কোড এবং হেডারস হ্যান্ডল করা একটি সাধারণ এবং গুরুত্বপূর্ণ কাজ। এটি সার্ভারের রেসপন্সের অবস্থা এবং অতিরিক্ত তথ্য (যেমন কুকি, কন্টেন্ট টাইপ, কন্টেন্ট লেংথ ইত্যাদি) বের করতে সহায়তা করে।

এখানে Apache HTTP Client ব্যবহার করে Response Status Code এবং Headers হ্যান্ডল করার বিস্তারিত পদ্ধতি আলোচনা করা হবে।


১. Response Status Code হ্যান্ডেল করা

HTTP Status Code সার্ভারের রেসপন্সের অবস্থা এবং প্রক্রিয়ার সফলতা বা ব্যর্থতা জানায়। যেমন:

  • 200 OK: রিকোয়েস্ট সফলভাবে সম্পন্ন হয়েছে।
  • 404 Not Found: রিকোয়েস্ট করা রিসোর্স পাওয়া যায়নি।
  • 500 Internal Server Error: সার্ভার সাইডে কোনো সমস্যা হয়েছে।

Apache HTTP Client দিয়ে আপনি HttpResponse অবজেক্ট থেকে getStatusLine() মেথড ব্যবহার করে স্ট্যাটাস কোড নিতে পারেন এবং তার ভিত্তিতে নির্দিষ্ট কার্যক্রম করতে পারেন।

Response Status Code হ্যান্ডেল করার উদাহরণ:

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpResponseStatusCodeExample {
    public static void main(String[] args) {
        String url = "https://jsonplaceholder.typicode.com/posts";

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);
            HttpResponse response = httpClient.execute(request);

            // Get the status code from the response
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("Response Status Code: " + statusCode);

            // Handling based on status code
            if (statusCode == 200) {
                // Successful response
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println("Response Body: " + responseBody);
            } else if (statusCode == 404) {
                System.out.println("Resource not found!");
            } else if (statusCode == 500) {
                System.out.println("Internal server error occurred!");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

এখানে:

  • response.getStatusLine().getStatusCode() মেথড ব্যবহার করে রেসপন্সের স্ট্যাটাস কোড পাওয়া হয়েছে।
  • স্ট্যাটাস কোড অনুসারে আপনি নির্দিষ্ট কার্যক্রম করতে পারেন, যেমন 200 OK হলে রেসপন্স প্রিন্ট করা, 404 ত্রুটি মেসেজ দেখানো ইত্যাদি।

আউটপুট (যদি স্ট্যাটাস কোড 200 হয়):

Response Status Code: 200
Response Body: [JSON response data]

২. HTTP Response Headers হ্যান্ডেল করা

HTTP Response Headers সার্ভার থেকে প্রাপ্ত অতিরিক্ত তথ্য প্রদান করে, যেমন Content-Type, Content-Length, Location (URL for redirection), Set-Cookie, Cache-Control ইত্যাদি। Apache HTTP Client ব্যবহার করে আপনি রেসপন্স হেডারস অ্যাক্সেস করতে পারেন এবং প্রয়োজন অনুযায়ী ডেটা হ্যান্ডল করতে পারেন।

Response Headers হ্যান্ডেল করার উদাহরণ:

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.Header;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpResponseHeadersExample {
    public static void main(String[] args) {
        String url = "https://jsonplaceholder.typicode.com/posts";

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);
            HttpResponse response = httpClient.execute(request);

            // Get all headers
            Header[] headers = response.getAllHeaders();
            System.out.println("Response Headers:");
            for (Header header : headers) {
                System.out.println(header.getName() + ": " + header.getValue());
            }

            // Accessing specific header (e.g., Content-Type)
            String contentType = response.getFirstHeader("Content-Type").getValue();
            System.out.println("Content-Type: " + contentType);

            // Processing the response body
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("Response Body: " + responseBody);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

এখানে:

  • response.getAllHeaders() মেথড দিয়ে সমস্ত রেসপন্স হেডারস পাওয়া হচ্ছে এবং একে একে প্রিন্ট করা হচ্ছে।
  • response.getFirstHeader("Content-Type") ব্যবহার করে নির্দিষ্ট একটি হেডারের মান বের করা হচ্ছে (এখানে Content-Type হেডারটি চেক করা হয়েছে)।

আউটপুট:

Response Headers:
Date: Thu, 10 Dec 2024 07:28:48 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 4587
...
Content-Type: application/json; charset=utf-8
Response Body: [JSON response data]

৩. Redirect Handling (Location Header)

HTTP রিডাইরেক্ট একটি সাধারণ ঘটনা, যেখানে সার্ভার আপনাকে অন্য URL এ রিডাইরেক্ট করে পাঠায় (যেমন 301 Moved Permanently, 302 Found ইত্যাদি)। Apache HTTP Client স্বয়ংক্রিয়ভাবে রিডাইরেক্ট হ্যান্ডল করে থাকে, তবে আপনি এটি কাস্টমাইজও করতে পারেন।

Redirect Handling Example:

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class RedirectHandlingExample {
    public static void main(String[] args) {
        String url = "http://httpbin.org/redirect/1";  // This URL will redirect to another URL

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);
            HttpResponse response = httpClient.execute(request);

            // Checking if it's a redirect
            if (response.getStatusLine().getStatusCode() == 301 || response.getStatusLine().getStatusCode() == 302) {
                String location = response.getFirstHeader("Location").getValue();
                System.out.println("Redirected to: " + location);
            }

            // Process the redirected content
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("Response Body: " + responseBody);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

এখানে:

  • response.getFirstHeader("Location") ব্যবহার করে রিডাইরেক্ট URL বের করা হচ্ছে, যেটি 301 বা 302 স্ট্যাটাস কোডের জন্য প্রযোজ্য।
  • রিডাইরেক্ট প্রক্রিয়াতে নতুন URL অ্যাক্সেস করা হয়েছে।

আউটপুট:

Redirected to: http://httpbin.org/get
Response Body: [JSON response data from the new location]

৪. Custom Headers Handling

অনেক সময় custom headers পাঠানো বা গ্রহণ করা হয়, যেমন Authorization, X-API-Key, ইত্যাদি। আপনি সহজেই কাস্টম হেডারস সেট করতে পারেন এবং রেসপন্স হেডারগুলোর মান পরীক্ষা করতে পারেন।

Custom Header Example:

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class CustomHeaderExample {
    public static void main(String[] args) {
        String url = "https://jsonplaceholder.typicode.com/posts";
        
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(url);
            
            // Setting a custom header
            request.setHeader("Authorization", "Bearer YOUR_ACCESS_TOKEN");

            HttpResponse response = httpClient.execute(request);

            // Checking for Authorization success
            if (response.getStatusLine().getStatusCode() == 200) {
                System.out.println("Authorization successful!");
            }

            // Processing the response body
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("Response Body: " + responseBody);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

এখানে:

  • request.setHeader("Authorization", "Bearer YOUR_ACCESS_TOKEN") মেথড ব্যবহার করে কাস্টম Authorization হেডার সেট করা হয়েছে।

সারাংশ

Apache HTTP Client দিয়ে Response Status Code এবং Headers হ্যান্ডল করা সহজ এবং কার্যকরী। আপনি HTTP রেসপন্সের স্ট্যাটাস কোড ব্যবহার করে সার্ভারের অবস্থা নির্ধারণ করতে পারেন এবং বিভিন্ন হেডারস (যেমন Content-Type, Location, Authorization ইত্যাদি) ব্যবহার করে সার্ভারের প্রাপ্ত অতিরিক্ত তথ্য বের করতে পারেন। Redirect handling, Custom headers এবং Error codes হ্যান্ডলিং এর মাধ্যমে আপনি আরও কাস্টমাইজড এবং কার্যকরী HTTP ক্লায়েন্ট তৈরি করতে পারবেন।

common.content_added_by
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion