使用Jsoup加代理发送get请求


  1. public static String get(String uri){  
  2.   
  3.   
  4.                     System.setProperty("https.proxySet""true");  
  5.             System.getProperties().put("https.proxyHost", ip);  
  6.             System.getProperties().put("https.proxyPort", port);  
  7.   
  8.   
  9.         Document doc = null;  
  10.         String  agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)"  
  11.                 + "  Chrome/56.0.2924.87 Safari/537.36" ;  
  12.         try {  
  13.             doc = Jsoup.connect(uri).ignoreContentType(true)  
  14.                     .userAgent(agent)  
  15.                     // ignoreHttpErrors   
  16.                     //这个很重要 否则会报HTTP error fetching URL. Status=404  
  17.                     .ignoreHttpErrors(true)  //这个很重要  
  18.                     .timeout(3000).get();  
  19.         } catch (IOException e) {  
  20.             System.out.println(e.getMessage()+"  **************** get");  
  21.         }  
  22.         if (doc!=null) {  
  23.             return doc.body().text();  
  24.         }  
  25.         return null;  
  26. }  

  1. public static String get(String uri){  
  2.   
  3.   
  4.                     System.setProperty("https.proxySet""true");  
  5.             System.getProperties().put("https.proxyHost", ip);  
  6.             System.getProperties().put("https.proxyPort", port);  
  7.   
  8.   
  9.         Document doc = null;  
  10.         String  agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)"  
  11.                 + "  Chrome/56.0.2924.87 Safari/537.36" ;  
  12.         try {  
  13.             doc = Jsoup.connect(uri).ignoreContentType(true)  
  14.                     .userAgent(agent)  
  15.                     // ignoreHttpErrors   
  16.                     //这个很重要 否则会报HTTP error fetching URL. Status=404  
  17.                     .ignoreHttpErrors(true)  //这个很重要  
  18.                     .timeout(3000).get();  
  19.         } catch (IOException e) {  
  20.             System.out.println(e.getMessage()+"  **************** get");  
  21.         }  
  22.         if (doc!=null) {  
  23.             return doc.body().text();  
  24.         }  
  25.         return null;  
  26. }  

猜你喜欢

转载自blog.csdn.net/hephec/article/details/79259301
今日推荐