Apache HTTP Client দিয়ে HTTP Response থেকে স্ট্যাটাস কোড এবং হেডারস হ্যান্ডল করা একটি সাধারণ এবং গুরুত্বপূর্ণ কাজ। এটি সার্ভারের রেসপন্সের অবস্থা এবং অতিরিক্ত তথ্য (যেমন কুকি, কন্টেন্ট টাইপ, কন্টেন্ট লেংথ ইত্যাদি) বের করতে সহায়তা করে।
এখানে Apache HTTP Client ব্যবহার করে Response Status Code এবং Headers হ্যান্ডল করার বিস্তারিত পদ্ধতি আলোচনা করা হবে।
HTTP Status Code সার্ভারের রেসপন্সের অবস্থা এবং প্রক্রিয়ার সফলতা বা ব্যর্থতা জানায়। যেমন:
Apache HTTP Client দিয়ে আপনি HttpResponse অবজেক্ট থেকে getStatusLine() মেথড ব্যবহার করে স্ট্যাটাস কোড নিতে পারেন এবং তার ভিত্তিতে নির্দিষ্ট কার্যক্রম করতে পারেন।
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();
}
}
}
এখানে:
আউটপুট (যদি স্ট্যাটাস কোড 200 হয়):
Response Status Code: 200
Response Body: [JSON response data]
HTTP Response Headers সার্ভার থেকে প্রাপ্ত অতিরিক্ত তথ্য প্রদান করে, যেমন Content-Type, Content-Length, Location (URL for redirection), Set-Cookie, Cache-Control ইত্যাদি। Apache HTTP Client ব্যবহার করে আপনি রেসপন্স হেডারস অ্যাক্সেস করতে পারেন এবং প্রয়োজন অনুযায়ী ডেটা হ্যান্ডল করতে পারেন।
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 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]
HTTP রিডাইরেক্ট একটি সাধারণ ঘটনা, যেখানে সার্ভার আপনাকে অন্য URL এ রিডাইরেক্ট করে পাঠায় (যেমন 301 Moved Permanently, 302 Found ইত্যাদি)। Apache HTTP Client স্বয়ংক্রিয়ভাবে রিডাইরেক্ট হ্যান্ডল করে থাকে, তবে আপনি এটি কাস্টমাইজও করতে পারেন।
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();
}
}
}
এখানে:
আউটপুট:
Redirected to: http://httpbin.org/get
Response Body: [JSON response data from the new location]
অনেক সময় custom headers পাঠানো বা গ্রহণ করা হয়, যেমন Authorization, X-API-Key, ইত্যাদি। আপনি সহজেই কাস্টম হেডারস সেট করতে পারেন এবং রেসপন্স হেডারগুলোর মান পরীক্ষা করতে পারেন।
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();
}
}
}
এখানে:
Apache HTTP Client দিয়ে Response Status Code এবং Headers হ্যান্ডল করা সহজ এবং কার্যকরী। আপনি HTTP রেসপন্সের স্ট্যাটাস কোড ব্যবহার করে সার্ভারের অবস্থা নির্ধারণ করতে পারেন এবং বিভিন্ন হেডারস (যেমন Content-Type, Location, Authorization ইত্যাদি) ব্যবহার করে সার্ভারের প্রাপ্ত অতিরিক্ত তথ্য বের করতে পারেন। Redirect handling, Custom headers এবং Error codes হ্যান্ডলিং এর মাধ্যমে আপনি আরও কাস্টমাইজড এবং কার্যকরী HTTP ক্লায়েন্ট তৈরি করতে পারবেন।
common.read_more