2014-04-16 46 views
1

我想使用Javas Jsoup库来抓取网页,但我背后的公司代理阻止我连接到网页。我研究了这个问题,并且现在知道我必须专门解决代理问题,并向代理确认我自己。但是我仍然无法连接到网页。我试图通过使用下面的代码在www.google.com上简单地检索标题来测试我的连接:无法使用公司代理后面的Jsoup连接到网站

import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 

public class Test { 
    public static void main(String[] args) { 
     System.out.println("1"); 
     try{    
      System.setProperty("http.proxyHost", "myProxy"); 
      System.setProperty("http.proxyPort", "myPort"); 
        System.setProperty("http.proxyUser", "myUser"); 
      System.setProperty("http.proxyPassword", "myPassword"); 

      Document doc = Jsoup.connect("http://google.com").get(); 
        String title = doc.title(); 
      System.out.println(title); 

      }catch(IOException e){ 
         System.out.println(e); 
      }   
     } 
    } 

上面的代码返回以下错误:

org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml. Mimetype=application/x-ns-proxy-autoconfig, URL= http://google.com

这告诉我,soemthing被检索但在无法处理的内容类型,所以我调整了“测试”忽略的内容类型,以便看到使用下面的代码是什么检索:

import java.io.IOException; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 

public class DemoII { 
    public static void main(String[] args) { 
     System.out.println("1"); 
     try{ 
        System.setProperty("http.proxyHost", "myProxy"); 
      System.setProperty("http.proxyPort", "myPort"); 
      System.setProperty("http.proxyUser", "myUser"); 
      System.setProperty("http.proxyPassword", "myPassword"); 

      String script = Jsoup.connect("http://google.com").ignoreContentType(true).execute().body(); 
      System.out.println(script); 
        }catch(IOException e){ 
         System.out.println(e); 
       }   
    } 
} 

它事实证明,“脚本”字符串从代理服务器中检索源代码。因此,我正在与代理建立一些连接,但我对www.google.com的请求未通过。任何想法我做错了什么?

+0

什么是代理的回应说? – MCL

+0

抱歉,由于公司政策,我无法发布源代码。一般而言,我取回定义代理如何运作的java代码。 – user3182273

+0

为什么选择Java代码?难道它会返回一个[Proxy auto-config](https://en.wikipedia.org/wiki/Proxy_auto-config)?我相信,将机密信息屏蔽掉不会是一个问题,是吗? – MCL

回答

0

OP找到一个解决方案:

@MCL hey thanks, i had no idea what this file did and after you told me what it does, i had a look inside and there was a proxy name that slightly differs from the one i used before, and now it works - Thanks – user3182273