【问题标题】:Java HttpURLConnection blocked at work, but not FirefoxJava HttpURLConnection 在工作中被阻止,但不是 Firefox
【发布时间】:2013-05-29 13:51:05
【问题描述】:

下午好,

我有一个关于 HttpURLConnection 和工作中的互联网限制的问题...

我想做的事:

我正在尝试编写一个程序来连接到站点http://www.epexspot.com 并读取电力的峰值和基准产品价格历史记录。


我为什么要这样做:

到目前为止,价格的收集都是手动完成的,这是一个繁琐的过程。 因此,我想用一个小程序来自动化它。


到目前为止我做了什么:

我编写了一个使用 HttpURLConnection 的 Java (JDK7u21) 程序,试图联系主页并获取发送的响应;在这里你几乎可以看到我写的内容:

HttpConnector.java

package network;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class HttpConnector {
String urlParameters, method;
URL url;
HttpURLConnection conn;
BufferedReader in;

public HttpConnector(String host, String method) throws IOException{
    if(!host.startsWith("http://") && !host.startsWith("https://"))
        host = "http://" + host;

    this.method = method;
    urlParameters = "";
    url = new URL(host);
}

public HttpConnector(String host, String method, String parameters) throws IOException{
    if(!host.startsWith("http://") && !host.startsWith("https://"))
        host = "http://" + host;

    this.method = method;
    urlParameters = parameters;
    url = new URL(host);
}

public void openConnection() throws IOException{
    conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod(method);
    conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0");
    conn.setRequestProperty("Host", url.getHost());
    conn.setRequestProperty("Connection", "keep-alive");
    if(urlParameters!="" && urlParameters!=null) 
        conn.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));
    conn.setRequestProperty("Accept-Language", "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3");
    conn.setRequestProperty("Accept-Encoding", "deflate");/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    conn.setUseCaches(false);
    conn.setDoInput(true);
    conn.setDoOutput(true);
}

public void sendRequest() throws IOException{
    if(method == "POST"){
        DataOutputStream out = new DataOutputStream(conn.getOutputStream());
        out.writeBytes(urlParameters);
        out.flush();
        out.close();
    }
}

public ArrayList<String> read() throws IOException{
    if(conn.getResponseCode()>226 || conn.getResponseCode()<200){
        try{
            in = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
        }catch(NullPointerException e){
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        }
    }else{
        in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    }
    ArrayList<String> resp = new ArrayList<String>();
    String respTmp;

    while((respTmp=in.readLine())!=null){
        resp.add(respTmp);
    }
    return resp;
}

public void close(){
    if(conn!=null) conn.disconnect();
}

public ArrayList<String> communicate() throws IOException{
    ArrayList<String> resp = new ArrayList<String>();
    try{
        openConnection();
        sendRequest();
        resp=read();
    }catch(Exception e){
        e.printStackTrace(System.err);
    }finally{
        close();
    }
    return resp;
}

}

Main.java

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;

import network.HttpConnector;


public class Main {
    public static void main(String[] args) {
        try{
            File f = new File("response.html");
            if(!f.exists()) f.createNewFile();

//          String host = "http://www.epexspot.com/en/market-data/auction/auction-table/2013-05-28/DE";
// this is where I actually need to go; google.at is merely for testing purposes
            String host = "www.google.at";
            String method = "GET";

            ArrayList<String> response = new ArrayList<String>();
            HttpConnector conn = new HttpConnector(host,method);
            response = conn.communicate();

            FileWriter fw = new FileWriter(f);
            BufferedWriter out = new BufferedWriter(fw);

            for(String resp : response){
                System.out.println(resp);
                out.write(resp+"\n");
            }

            out.flush();
            out.close();
            fw.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}

简短说明: HttpConnector 使用给定的方法(主要是 POST 或 GET)和给定的 URL 参数(虽然我不使用)连接到给定的主机。 它设置一些请求属性(例如 User-Agent,...),然后尝试读取响应(通过 InputStream;如果响应状态显示不成功,则通过 ErrorStream)。

Main 使用特定 URL(例如 www.epexspot.com/en/)和特定方法(POST 或 GET)调用 HttpConnector。 然后它读取连接的响应并将其打印到控制台以及文件 (response.html)。


我的问题出在哪里:

在工作中,流量是受管制的,这意味着某些主页被屏蔽(就像它们在学校被屏蔽的方式一样)。所以,当然,如果我将一些社交媒体平台的 URL 提供给我的小程序,它会吐出类似“错误 403 - 页面内容已被阻止。如果您需要此页面工作,请联系您的管理员”。

例如,当我尝试访问所需的页面 epexspot.com - 但是时,我会遇到这种情况: 当我使用普通的 Mozilla Firefox (v21) 调用该页面时,该页面被阻止。 在某些页面上,我的程序可以正常工作,但不能在大多数页面上正常工作(例如 www.google.at、www.ivb.at 工作正常......而大多数其他页面则不行)

我已经尝试让我的程序在请求属性方面表现得像 Firefox,但到目前为止它没有导致任何结果...... 我是否遗漏了一些请求属性或设置,这些属性或设置可能会使互联网监管软件阻止我的程序,但不会阻止 Mozilla Firefox?


所以,我的主要问题是:

我的程序一直被阻止的原因是什么,而 Firefox 在它附近的任何地方都不会遇到阻止级别?

我会尝试联系工作中的网络管理员,希望他们有一个解决方案,让我的程序不再一直被阻止,但我仍然想知道是什么让 Firefox 和我的程序之间产生了如此显着的差异。


提前致谢

【问题讨论】:

  • Firefox 是否配置为使用代理?
  • 啊,是的,当然……这对我来说当然太明显了;它确实设置为自动配置
  • 感谢@jtahlborn 向我展示了我没有看到的显而易见的东西!
  • 没问题,有时显而易见的东西是最难看到的。 :)

标签: java firefox proxy httpurlconnection blocked


【解决方案1】:

好吧,答案就这么简单……

Firefox 已被配置为使用自动配置的代理服务器,所以这是我所做的:

我在 Firefox 中打开了我的网站,使用了 netstat -an | find "EST" 技巧,弄清楚了代理的地址(和端口)是什么,并让我的程序将它们与这些行一起使用

System.setProperty("http.proxyHost", proxyAddress);System.setProperty("http.proxyPort", "8080");

这解决了我的问题...

感谢 jtahlborn 的提示!

编辑:

使用 ProxySelector 也很有效;如果您需要它,请点击此链接:http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-19
    • 2014-05-17
    • 2018-07-03
    • 2016-01-29
    相关资源
    最近更新 更多