Java HttpURLConnection在工作时被阻止,但Firefox未被阻止
下午好 我有一个关于HttpUrl连接和工作中的互联网限制的问题 我想做什么: 我试图写一个程序,连接到该网站,并读取峰值和基础产品的电力价格历史Java HttpURLConnection在工作时被阻止,但Firefox未被阻止,java,firefox,proxy,httpurlconnection,blocked,Java,Firefox,Proxy,Httpurlconnection,Blocked,下午好 我有一个关于HttpUrl连接和工作中的互联网限制的问题 我想做什么: 我试图写一个程序,连接到该网站,并读取峰值和基础产品的电力价格历史 我为什么要这么做: 到目前为止,价格的收集是手工完成的,这是一个繁琐的过程。 因此,我想用一个小程序来实现自动化 到目前为止我所做的: 我使用HttpURLConnection编写了一个Java(JDK7u21)程序,试图联系主页并获取发送的响应;在这里,你可以看到我写的内容: HttpConnector.java package network
我为什么要这么做: 到目前为止,价格的收集是手工完成的,这是一个繁琐的过程。 因此,我想用一个小程序来实现自动化
到目前为止我所做的: 我使用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;
}
}
封装网络;
导入java.io.BufferedReader;
导入java.io.DataOutputStream;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.URL;
导入java.util.ArrayList;
公共类HttpConnector{
字符串参数,方法;
网址;
httpurl连接连接;
缓冲读取器;
公共HttpConnector(字符串主机、字符串方法)引发IOException{
如果(!host.startsWith(“http://”)&&!host.startsWith(“https://”))
host=“http://”+主机;
这个方法=方法;
urlParameters=“”;
url=新url(主机);
}
公共HttpConnector(字符串主机、字符串方法、字符串参数)引发IOException{
如果(!host.startsWith(“http://”)&&!host.startsWith(“https://”))
host=“http://”+主机;
这个方法=方法;
urlParameters=参数;
url=新url(主机);
}
public void openConnection()引发IOException{
conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(方法);
conn.setRequestProperty(“用户代理”、“Mozilla/5.0(Windows NT 6.1;rv:21.0)Gecko/20100101 Firefox/21.0”);
conn.setRequestProperty(“主机”,url.getHost());
conn.setRequestProperty(“连接”、“保持活动”);
if(urlParameters!=“”&&urlParameters!=null)
conn.setRequestProperty(“内容长度”,Integer.toString(urlParameters.getBytes().Length));
conn.setRequestProperty(“接受语言”,“de-de,de;q=0.8,en-us;q=0.5,en;q=0.3”);
conn.setRequestProperty(“接受编码”、“泄气”);/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”);
conn.SETUSECHACHES(假);
conn.setDoInput(真);
连接设置输出(真);
}
public void sendRequest()引发IOException{
如果(方法==“POST”){
DataOutputStream out=新的DataOutputStream(conn.getOutputStream());
out.writeBytes(urlParameters);
out.flush();
out.close();
}
}
public ArrayList read()引发IOException{
如果(conn.getResponseCode()>226 | | conn.getResponseCode()好的话,答案很简单
Firefox已配置为使用自动配置的代理服务器,因此我所做的如下:
我让我的网站在Firefox中打开,做了netstat-an | find“EST”
的把戏,弄清楚代理的地址(和端口)是什么,并让我的程序将它们与行一起使用
System.setProperty(“http.proxyHost”,proxyAddress);
和
System.setProperty(“http.proxyPort”、“8080”);
这解决了我的问题
谢谢jtahlborn的提示
编辑:
使用ProxySelector也可以很好地工作;如果您需要它,请点击以下链接:firefox是否配置为使用代理?啊,是的,当然……这对我来说太明显了,我看不到;它确实设置为自动配置数据库,@jtahlborn,用于向我显示我看不到的明显内容!没问题,有时明显的是最难看的东西。:)
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();
}
}
}