我将以下简单代码编程为自动将数据输入网站。但是,我收到以下错误:
Exception in thread "main" java.net.ConnectException: Connection refused: connect
我正在使用NetBeans。
代码:
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Scanner;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.*;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.*;
public class ts {
public static void main(String[] args) throws IOException{
final WebClient webClient = new WebClient();
HtmlPage page = (HtmlPage)webClient.getPage( new URL("http://rise4fun.com/QuickCode") );
final HtmlForm form = (HtmlForm)page.getFormsByName("form1").get(0);
final HtmlInput inp = form.getInputByName("SourceBox");
inp.setValueAttribute("24.9.2011 | 24 Sep 2011\n8/15/2010");
HtmlSubmitInput sub=(HtmlSubmitInput) page.getHtmlElementById("AskButton");
page = (HtmlPage)sub.submit();
final String pageAsText = page.asText();
HtmlElement out=page.getHtmlElementById("OutputBox");
System.out.println(out.asText());
}
}
答案 0 :(得分:1)
由于我现在知道你在防火墙后面,你的Java代码必须先定义代理才能访问互联网。
URL url = new URL("http://rise4fun.com/QuickCode");
System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort);
URLConnection connection = url.openConnection();
编写代理的另一种方法是:
URL url = new URL("http://rise4fun.com/QuickCode");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy ip", 8080));
URLConnection connection = url.openConnection(proxy);