无法使用公司代理后面的Jsoup连接到网站

时间:2014-04-16 08:21:05

标签: java html proxy jsoup

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

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:未处理的内容类型。必须是text / *,application / xml或application / xhtml + xml。 Mimetype = application / x-ns-proxy-autoconfig,URL = http://google.com

这告诉我,soemthing被检索但是内容类型无法处理,所以我调整了#34;测试"忽略内容类型,以便使用以下代码查看检索内容:

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);
              }         
    }
}

事实证明"脚本" string从代理服务器检索源代码。所以我与代理建立了一些联系,但我对www.google.com的请求没有通过。我有什么想法吗?

1 个答案:

答案 0 :(得分:0)

OP找到了解决方案:

  

@MCL嘿谢谢,我不知道这个文件做了什么,在你告诉我它做了什么之后,我看了一眼内部,并且有一个代理名称与我之前使用的名称略有不同,并且现在它有效 - 谢谢 - user3182273

相关问题