2013-01-22 156 views
0

我有以下編程簡單的代碼來自動輸入數據到網站。不過,我得到以下錯誤:Java連接被拒絕

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

您是否能以其他方式訪問的網址是什麼? – Swapnil

+0

@ Swapnil-是的,我可以訪問網址 – user1628340

回答

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

does'nt help ... m根本無法訪問任何網站 – user1628340

+0

您是否在防火牆後面? –

+0

不,我允許Netbeans通過我的防火牆 – user1628340