【转】JAVA 验证代理是否可用

苦苦在网上找了一天,先后试了四种方法,最终实现,在此做一总结

方法一:

HttpClient 方法   
    HttpClient client=new HttpClient();
    client.getHostConfiguration().setHost("111432.1.32.93", 80, "http");   
    HttpMethod method =new GetMethod("http://www.07073.com/gld/");
   int statusCode = client.executeMethod(method);   
   System.out.println("---------"+statusCode+"------------");

方法二:

JAVA设置JVM代理方法
    System.getProperty("proxySet", "true");
         System.getProperties().setProperty("http.proxyHost", "1532603.11.62.176");
         System.getProperties().setProperty("http.proxyPort", "810");
         System.out.println(getHtml("http://www.07073.com/gld/")); 
         

private String getHtml(String address){
   StringBuffer html = new StringBuffer();
   String result = null;
  
   URL url;
   try {
    url = new URL(address);
    URLConnection conn = url.openConnection();

    conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; GTB5; .NET CLR 2.0.50727; CIBA)");

    BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
   
    try {
      String inputLine; 
      byte[] buf = new byte[4096]; 
      int bytesRead = 0; 
      while (bytesRead >= 0) { 
      inputLine = new String(buf, 0, bytesRead, "ISO-8859-1"); 
      html.append(inputLine); 
      bytesRead = in.read(buf); 
      inputLine = null;
     }
      buf = null;
     } finally {
      in.close();
      conn = null;
      url = null;
     }
     result = new String(html.toString().trim().getBytes("ISO-8859-1"), "gb2312").toLowerCase();

   } catch (Exception e) {
    e.printStackTrace();
    return null;
   }
   html=null;
   return result;
}

方法三:

JAVA PING网址方法
    Process p = Runtime.getRuntime().exec("ping "+ip);
    InputStream is = p.getInputStream();   
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));   
    String line;   
    String time=null;
    String temp=null;
      while ((line = reader.readLine()) != null) {   
        temp+=line;
        //判断是否丢包
        if(line.indexOf("out")>0){
         flag=false;
         break;
        }
        //取得延时
        if(line.indexOf("time")>0&&line.indexOf("ms")>0){
         time=line.substring(line.indexOf("time")+5,line.indexOf("ms"));
         System.out.println(line.substring(line.indexOf("time")+5,line.indexOf("ms")));
      //如果延时小于5秒
         if(Integer.valueOf(time)<5000)
        flag=true;
        else
        flag=false;
         break;
        }
      }   
      System.out.println(temp);
      is.close();   
      reader.close();   
      p.destroy();   
         System.out.println(getHtml("http://www.07073.com/gld/"));
         method.eleaseConnection();

方法四:

    //Proxy类代理方法
         URL url = new URL("http://www.baidu.com");
        // 创建代理服务器
        InetSocketAddress addr=null;
        addr=new InetSocketAddress("61.135.149.177",8888);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理
        URLConnection conn = url.openConnection(proxy);
           InputStream in = conn.getInputStream();
           String s = IOUtils.toString(in);
           //System.out.println(s);
           if(s.indexOf("百度")>0){
            System.out.println("ok");
           }

前二种方法都会因代理不好用,而程序自动采用本机IP访问页面,所以也验证不出代理是否可用,而第三种方法也只能测试代理的速度,也没有实际效用,第四种方法对于设置代理后无法访问页面,得不到页面流时会报错,可采用

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
MQTT是一种轻量级的消息传递协议,它适用于物联网等低带宽、不可靠网络环境。Java中有多个MQTT客户端库可用,例如Eclipse Paho和HiveMQ等。使用这些客户端库,Java开发人员可以轻松地使用MQTT协议与MQTT代理进行通信。 以下是使用Eclipse Paho Java客户端库实现MQTT通信的示例代码: ```java import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; public class MqttClientExample { public static void main(String[] args) throws MqttException, InterruptedException { String brokerUrl = "tcp://localhost:1883"; String clientId = "JavaClient"; MemoryPersistence persistence = new MemoryPersistence(); MqttClient client = new MqttClient(brokerUrl, clientId, persistence); MqttConnectOptions options = new MqttConnectOptions(); options.setCleanSession(true); client.connect(options); String topic = "test"; String message = "Hello, MQTT!"; client.publish(topic, new MqttMessage(message.getBytes())); client.subscribe(topic, (topicName, messageReceived) -> { System.out.println("Received message: " + new String(messageReceived.getPayload())); }); Thread.sleep(5000); client.disconnect(); } } ``` 这个例子中,我们使用Eclipse Paho Java客户端库创建了一个MQTT客户端,连接到本地的MQTT代理,发布了一个消息,并订阅了同一个主题以接收消息。在订阅主题后,我们使用Lambda表达式定义了消息到达时的回调函数。最后,我们等待5秒钟后断开了与MQTT代理的连接。 需要注意的是,在使用MQTT协议时,需要考虑到消息传递的可靠性和安全性问题。例如,在QoS级别为0时,消息可能会因为网络问题而丢失;在QoS级别为1或2时,需要考虑到消息重传和重复消息的问题。此外,在使用MQTT时,还需要考虑到身份验证、访问控制、数据加密等安全问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值