0

我编写了以下简单的代码来自动将数据输入网站。但是,我收到以下错误:

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());

    }

}
4

1 回答 1

1

由于我现在知道您在防火墙后面,因此您的 Java 代码必须在您可以访问 Internet 之前定义代理。

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);
于 2013-01-22T17:49:30.710 回答