1

こんにちは、

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 と私のプログラムにこれほど大きな違いが生じる理由は何なのか、まだ疑問に思っています。


前もって感謝します

4

1 に答える 1

0

さて、答えは可能な限り簡単です...

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

于 2013-05-31T06:04:27.497 に答える