একটি বিশেষ URL এর প্রতিক্রিয়া কোড পেতে দয়া করে আমাকে পদক্ষেপগুলি বা কোডটি বলুন the
একটি বিশেষ URL এর প্রতিক্রিয়া কোড পেতে দয়া করে আমাকে পদক্ষেপগুলি বা কোডটি বলুন the
উত্তর:
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
এটি কোনওভাবেই দৃ example় উদাহরণ নয়; আপনাকে হ্যান্ডেল করতে হবে IOException
এবং কী হবে না। তবে এটি আপনার শুরু করা উচিত।
আপনার যদি আরও দক্ষতার সাথে কিছু প্রয়োজন হয় তবে এইচটিটিপিপ্লায়েন্টটি দেখুন ।
disconnect()
পদ্ধতি অন্তর্নিহিত সকেট বন্ধ করে দিতে পারেন যদি ক্রমাগত সংযোগ অন্যথায় যে সময়ে নিষ্ক্রিয়। , যা গ্যারান্টি দেয় না। দস্তাবেজগুলি এও ইঙ্গিত করে যে সার্ভারের কাছে অন্যান্য অনুরোধগুলি অদূর ভবিষ্যতে অসম্ভব। কল করার disconnect()
অর্থ এই নয় যে এই HttpURLConnection
অনুরোধটি অন্য অনুরোধগুলির জন্য পুনরায় ব্যবহার করা যেতে পারে। আপনি যদি InputStream
ডেটা পড়ার জন্য ব্যবহার করছেন তবে আপনার close()
সেই finally
ব্লকটি স্ট্রিম করা উচিত ।
URL url = new URL("http://www.google.com/humans.txt");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
আপনি নিম্নলিখিত চেষ্টা করতে পারেন:
class ResponseCodeCheck
{
public static void main (String args[]) throws Exception
{
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
System.out.println("Response code of the object is "+code);
if (code==200)
{
System.out.println("OK");
}
}
}
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;
public class API{
public static void main(String args[]) throws IOException
{
URL url = new URL("http://www.google.com");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
System.out.println(statusCode);
}
}
এটি আমার পক্ষে কাজ করেছে:
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public static void main(String[] args) throws Exception {
HttpClient client = new DefaultHttpClient();
//args[0] ="http://hostname:port/xyz/zbc";
HttpGet request1 = new HttpGet(args[0]);
HttpResponse response1 = client.execute(request1);
int code = response1.getStatusLine().getStatusCode();
try(BufferedReader br = new BufferedReader(new InputStreamReader((response1.getEntity().getContent())));){
// Read in all of the post results into a String.
String output = "";
Boolean keepGoing = true;
while (keepGoing) {
String currentLine = br.readLine();
if (currentLine == null) {
keepGoing = false;
} else {
output += currentLine;
}
}
System.out.println("Response-->"+output);
}
catch(Exception e){
System.out.println("Exception"+e);
}
}
এটিই আমার পক্ষে কাজ করেছে:
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlHelpers {
public static int getHTTPResponseStatusCode(String u) throws IOException {
URL url = new URL(u);
HttpURLConnection http = (HttpURLConnection)url.openConnection();
return http.getResponseCode();
}
}
আশা করি এটি কাউকে সাহায্য করবে :)
এই কোডটির টুকরোটি চেষ্টা করুন যা 400 ত্রুটি বার্তাগুলি পরীক্ষা করছে
huc = (HttpURLConnection)(new URL(url).openConnection());
huc.setRequestMethod("HEAD");
huc.connect();
respCode = huc.getResponseCode();
if(respCode >= 400) {
System.out.println(url+" is a broken link");
} else {
System.out.println(url+" is a valid link");
}
স্ক্যানার দ্বারা ডেটা পাওয়ার জন্য কার্যকর উপায় (অসম পেলোড সহ)।
public static String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A"); // Put entire content to next token string, Converts utf8 to 16, Handles buffering for different width packets
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
} finally {
urlConnection.disconnect();
}
}
এটি সম্পূর্ণ স্থিতিশীল পদ্ধতি, যা আইওএক্সেপশন ঘটলে আপনি অপেক্ষার সময় এবং ত্রুটি কোড সেট করতে মানিয়ে নিতে পারেন:
public static int getResponseCode(String address) {
return getResponseCode(address, 404);
}
public static int getResponseCode(String address, int defaultValue) {
try {
//Logger.getLogger(WebOperations.class.getName()).info("Fetching response code at " + address);
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(1000 * 5); //wait 5 seconds the most
connection.setReadTimeout(1000 * 5);
connection.setRequestProperty("User-Agent", "Your Robot Name");
int responseCode = connection.getResponseCode();
connection.disconnect();
return responseCode;
} catch (IOException ex) {
Logger.getLogger(WebOperations.class.getName()).log(Level.INFO, "Exception at {0} {1}", new Object[]{address, ex.toString()});
return defaultValue;
}
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
। । । । । । ।
System.out.println("Value" + connection.getResponseCode());
System.out.println(connection.getResponseMessage());
System.out.println("content"+connection.getContent());
ওয়েবসাইট এবং অন্যান্য তথ্য থেকে প্রতিক্রিয়া কোড পেতে আপনি জাভা http / https url সংযোগটি ব্যবহার করতে পারেন পাশাপাশি একটি নমুনা কোড is
try {
url = new URL("https://www.google.com"); // create url object for the given string
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(https_url.startsWith("https")){
connection = (HttpsURLConnection) url.openConnection();
}
((HttpURLConnection) connection).setRequestMethod("HEAD");
connection.setConnectTimeout(50000); //set the timeout
connection.connect(); //connect
String responseMessage = connection.getResponseMessage(); //here you get the response message
responseCode = connection.getResponseCode(); //this is http response code
System.out.println(obj.getUrl()+" is up. Response Code : " + responseMessage);
connection.disconnect();`
}catch(Exception e){
e.printStackTrace();
}
এটি একটি পুরানো প্রশ্ন, তবে বিশ্রামের উপায়ে (জ্যাক্স-আরএস) দেখাতে দেয়:
import java.util.Arrays;
import javax.ws.rs.*
(...)
Response response = client
.target( url )
.request()
.get();
// Looking if response is "200", "201" or "202", for example:
if( Arrays.asList( Status.OK, Status.CREATED, Status.ACCEPTED ).contains( response.getStatusInfo() ) ) {
// lets something...
}
(...)