user1628340
user1628340

Reputation: 941

Java Connection refused

I have the following simple code programmed to automatically enter data to a website. However, I get the following error:

Exception in thread "main" java.net.ConnectException: Connection refused: connect

I am using NetBeans.

Code:

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

    }

}

Upvotes: 0

Views: 1010

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

Since I now know that you're behind a firewall, your Java code has to define the proxy before you can access the Internet.

URL url = new URL("http://rise4fun.com/QuickCode");
System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort);
URLConnection connection = url.openConnection();

Another way of coding for a proxy is:

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

Upvotes: 1

Related Questions