请告诉我获取特定网址的响应代码的步骤或代码。
答案 0 :(得分:164)
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
这绝不是一个有力的例子;你需要处理IOException
和诸如此类的东西。但它应该让你开始。
如果您需要具有更多功能的内容,请查看HttpClient。
答案 1 :(得分:36)
URL url = new URL("http://www.google.com/humans.txt");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
答案 2 :(得分:10)
您可以尝试以下方法:
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");
}
}
}
答案 3 :(得分:5)
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);
}
}
答案 4 :(得分:4)
这对我有用:
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);
}
}
答案 5 :(得分:2)
这对我有用:
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();
}
}
希望这有助于某人:)
答案 6 :(得分:2)
尝试检查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");
}
答案 7 :(得分:2)
这是一个完整的静态方法,当发生IOException时,您可以使用它来设置等待时间和错误代码:
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;
}
}
答案 8 :(得分:0)
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());
答案 9 :(得分:0)
您可以使用java http / https url连接从网站获取响应代码和其他信息以及此处的示例代码。
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();
}
答案 10 :(得分:0)
通过扫描仪获取数据(有效载荷不均匀)的有效方法。
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();
}
}
答案 11 :(得分:0)
这是一个古老的问题,但让我们以REST方式(JAX-RS)进行展示:
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...
}
(...)