Rob*_*ska 170
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
Run Code Online (Sandbox Code Playgroud)
这绝不是一个有力的例子; 你需要处理IOException
s等等.但它应该让你开始.
如果您需要具有更多功能的东西,请查看HttpClient.
小智 37
URL url = new URL("http://www.google.com/humans.txt");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
int statusCode = http.getResponseCode();
Run Code Online (Sandbox Code Playgroud)
小智 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 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);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
241561 次 |
最近记录: |