请告诉我获取特定网址URL响应代码的步骤或代码。如何获取Java中的URL的HTTP响应代码?
回答
URL url = new URL("http://example.com");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
这决不是一个坚固的例子;你需要处理IOException
和什么。但它应该让你开始。
如果您需要更多功能的东西,请查看HttpClient。
在我的具体情况和您的方法中,我得到一个IOException(“无法通过代理进行身份验证”),通常是一个HTTP错误407。有没有一种方法可以获得有关异常的精确(http错误代码)由getRespondeCode()方法引发?顺便说一句,我知道如何处理我的错误,我只想知道如何区分每个异常(或至少这个特定的异常)。谢谢。 – grattmandu03
@ grattmandu03 - 我不确定。看起来像你遇到http://stackoverflow.com/questions/18900143/getting-http-407-error-as-an-ioexception(很遗憾没有答案)。你可以尝试使用像HttpClient这样的更高级别的框架,这可能会让你更好地控制你如何处理这样的响应。 –
好的,谢谢你的回答。我的工作是调整一个旧代码来处理这个代理,并且更少的修改让客户理解我的工作。但我想,这对我来说(现在)是我想做的唯一方法。不管怎么说,还是要谢谢你。 – grattmandu03
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");
}
}
}
在线程“main”中获取异常java.net.ConnectException:连接被拒绝:连接。我不知道分享 –
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);
}
}
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());
我们如何处理基本身份验证的网址? –
这是对我工作:
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();
}
}
希望这可以帮助别人:)
这为我工作:
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);
}
}
您可以使用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();
}
通过扫描仪获取数据(有效载荷不均匀)的有效方法。
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();
}
}
试试这个代码和平被检查的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");
}
- 1. Java HTTP响应代码,URL,IOException
- 2. Java,如何从HTTP POST方法获取连接响应代码?
- 3. 如何获取java http请求中的代理响应?
- 4. 快速获取Url的http响应代码
- 5. 如何仅从Ruby中的HTTP请求获取响应代码
- 6. 如何获得http响应代码
- 7. 获取HTTP响应代码在MUnit
- 8. 使用python获取http响应代码
- 9. Retrofit/RxJava - 获取http响应代码
- 10. 从网址获取http响应代码
- 11. PHP获取HTTP响应代码
- 12. 获取Java中HTTP响应的大小
- 13. 如何在JQuery POST中获取HTTP错误响应代码
- 14. 如何在soap客户端中获取Http响应代码?
- 15. HTTP响应码:400的URL?
- 16. 在java中通过http读取文件:HTTP响应代码414
- 17. 如何获取XMLHttpRequest中的响应URL?
- 18. 如何使用Java HTTP请求代码获取Yodlee FastLink URL?
- 19. python,当http响应为None时,如何获得响应代码
- 20. 如何从浏览器活动获取http响应代码?
- 21. 从java HTTP响应中获取正确的类型/编码
- 22. 如何通过Java中的套接字获取HTTP响应?
- 23. 在Textview中获取响应,Url和Url代码
- 24. 如何从VolleyError获取响应代码?
- 25. 如何在node.js中获取http响应?
- 26. 针对不同HTTP响应代码的示例http url
- 27. 从Java中获取LDAP(OpenDS)中的其他响应代码
- 28. 获取html响应的HTTP响应
- 29. Http获取响应
- 30. 获取代码名为1的http响应代码的最佳方法
看到这个:HTTP://www.codingdiary.com/developers/developers/diary/javaapi/java/net/ SampleCode/HttpURLConnectionGetResponseCodeExampleCode.html –
我不会说重复,因为他想要响应代码,但@Ajit你应该检查出来。添加一点实验,你很好去。 – slezica
而不是要求其他人为你做你的工作。请证明你至少试图完成你自己的任务。显示您当前的代码以及您如何尝试完成此任务。如果你想要一个人为你做你的工作,而你没有努力,你可以聘请一个人并付款。 –