「javaurl状态码」javaurl编码方式

博主:adminadmin 2022-12-01 22:40:06 89

本篇文章给大家谈谈javaurl状态码,以及javaurl编码方式对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

怎样用java获取URL返回状态码

可以用以下代码参考一下,如何获取URL返回状态码:

//  用getResponseCode可以获取URL返回状态码

String surl = "";

try {

           surl="你的url";

               URL url = new URL(surl);

               URLConnection rulConnection   = url.openConnection();

               HttpURLConnection httpUrlConnection  =  (HttpURLConnection) rulConnection;

               httpUrlConnection.setConnectTimeout(300000);

               httpUrlConnection.setReadTimeout(300000);

               httpUrlConnection.connect();

               String code = new Integer(httpUrlConnection.getResponseCode()).toString();

               String message = httpUrlConnection.getResponseMessage();

               System.out.println("getResponseCode code ="+ code);

               System.out.println("getResponseMessage message ="+ message);

               if(!code.startsWith("2")){

                    throw new Exception("ResponseCode is not begin with 2,code="+code);

               }

               System.out.println(getCurDateTime()+"连接"+surl+"正常");

          }catch(Exception ex){

               System.out.println(ex.getMessage());

          }

java获取url状态码问题

httpclient默认自动处理redirect,返回200是因为它已经自动跳转了,如果想抓到301状态,可以手动修改跳转规则(以下代码基于httpclient 4.5):

public static void main(String[] args) {

        CloseableHttpClient 

httpclient = HttpClients.custom().setRedirectStrategy(new 

DefaultRedirectStrategy() {                

            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)  {

                return false;

            }}).build();

        HttpClientContext context = HttpClientContext.create();

        HttpGet httpget = new HttpGet("

);

        CloseableHttpResponse response = null;

        try {

            response = httpclient.execute(httpget, context);

            HttpHost target = context.getTargetHost();

            ListURI redirectLocations = context.getRedirectLocations();

            URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);

            System.out.println("Final HTTP location: " + location.toASCIIString());

            System.out.println(response.getStatusLine());

            // Expected to be an absolute URI

        } catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } finally {

            try {

                response.close();

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

    }

java 用 HttpClients 请求 url 返回状态码是 503 怎么处理

// 用getResponseCode可以获取a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9rHnkmWD3nWbdnANBnj-W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHRYnHndP1fdPH6Ynj0snWfzPs" target="_blank" class="baidu-highlight"URL/a返回状态码

String sa href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9rHnkmWD3nWbdnANBnj-W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHRYnHndP1fdPH6Ynj0snWfzPs" target="_blank" class="baidu-highlight"url/a = "";

try {

sa href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9rHnkmWD3nWbdnANBnj-W0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHRYnHndP1fdPH6Ynj0snWfzPs" target="_blank" class="baidu-highlight"url/a="你的url";

URL url = new URL(surl);

URLConnection rulConnection = url.openConnection();

HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection;

httpUrlConnection.setConnectTimeout(300000);

httpUrlConnection.setReadTimeout(300000);

httpUrlConnection.connect();

String code = new Integer(httpUrlConnection.getResponseCode()).toString();

String message = httpUrlConnection.getResponseMessage();

System.out.println("getResponseCode code ="+ code);

System.out.println("getResponseMessage message ="+ message);

if(!code.startsWith("2")){

throw new Exception("ResponseCode is not begin with 2,code="+code);

}

System.out.println(getCurDateTime()+"连接"+surl+"正常");

}catch(Exception ex){

System.out.println(ex.getMessage());

}

关于javaurl状态码和javaurl编码方式的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-01,除非注明,否则均为首码项目网原创文章,转载请注明出处。