测试网址是否能访问

 
  
function httpcode($url){
    $ch = curl_init();
    $timeout = 3;
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_exec($ch);
    return $httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
    curl_close($ch);
}
 
  

 

public function cs_url(){
        set_time_limit(0);
        $video=Db('')->field('')->select();
        $trues=[];
        $falses=[];
        foreach ($video as $v){
           // $m3u8=substr($v['m3u8'],5,count($v['m3u8'])-2).'8';
            $v['code']=httpcode($v['m3u8']);
           // print_r($v['code']);
            if($v['code']==200){
                $trues[]=$v;
            }else{
                $falses[]=$v;
            }
        }
        return json(array('code'=>0,'msg'=>'success','trues'=>$trues,'falses'=>$falses));
    }

 

转载于:https://www.cnblogs.com/zrn-php/p/9361667.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Java 中,可以使用 HttpURLConnection 或 HttpClient 来测试 https 网址是否访问。下面分别介绍这两种方法的具体实现: 1. 使用 HttpURLConnection: ```java import java.net.HttpURLConnection; import java.net.URL; public class HttpsTester { public static void main(String[] args) { String httpsUrl = "https://example.com"; try { URL url = new URL(httpsUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.connect(); int code = conn.getResponseCode(); if (code == 200) { System.out.println(httpsUrl + " is available!"); } else { System.out.println(httpsUrl + " is not available!"); } } catch (Exception e) { System.out.println(httpsUrl + " is not available!"); } } } ``` 2. 使用 HttpClient: ```java import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.impl.client.HttpClients; public class HttpsTester { public static void main(String[] args) { String httpsUrl = "https://example.com"; try { HttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); HttpGet httpGet = new HttpGet(httpsUrl); HttpResponse response = httpClient.execute(httpGet); int code = response.getStatusLine().getStatusCode(); if (code == 200) { System.out.println(httpsUrl + " is available!"); } else { System.out.println(httpsUrl + " is not available!"); } } catch (Exception e) { System.out.println(httpsUrl + " is not available!"); } } } ``` 需要注意的是,在测试 https 网址时,需要考虑证书的验证问题。如果网站使用了自签名证书或者是不受信任的证书,那么需要在 HttpClient 中设置 NoopHostnameVerifier,才能正常访问
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值