【问题标题】:Can not connect to website using Jsoup behind company proxy无法使用公司代理后面的 Jsoup 连接到网站
【发布时间】:2014-05-30 23:50:08
【问题描述】:

我想使用 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

这告诉我某些东西已被检索,但其内容类型无法处理,因此我调整了“测试”以忽略该内容类型,以便使用以下代码查看检索到的内容:

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 的请求没有通过。任何想法我做错了什么?

【问题讨论】:

  • 代理的响应是什么意思?
  • 对不起,由于公司政策,我无法发布源代码。一般而言,我会返回定义代理如何运行的 java 代码。
  • 为什么是Java代码?难道它返回一个Proxy auto-config?而且我确信屏蔽机密信息不会有问题,不是吗?
  • Document doc = Jsoup.connect("http://google.com").ignoreContentType(true).get(); 产生了什么?
  • @MCL 是的,在我看来你是对的,我得到了代理自动配置。

标签: java html proxy jsoup


【解决方案1】:

OP 找到了解决方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-13
    • 2013-11-06
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2014-10-28
    相关资源
    最近更新 更多