2011-04-07 24 views
2

我有當用戶在其卡上的JButton,它連接到一個PHP腳本在我的網站Swing應用程序將一些數據從PHP發送和檢索結果腳本。問題連接到PHP腳本與Java應用程序

這工作得很好了誰使用這個應用程序的用戶的100S,但今天在公司中的用戶的一個報告說,當他點擊按鈕的應用程序掛起並沒有任何反應,他不能用這個...。

我甚至用的UncaughtExceptionHandler來處理應用程序中的任何意外的異常,但沒有什麼異常。我認爲這可能是他公司的網絡或使用的端口,但我不確定。 有什麼建議,爲什麼會發生這種情況?

這裏是我的代碼:

String part1 = "..."; // Message part 1. 
String part2 = "..."; // Message part 2. 

//1. Encode the message to suite the URL path requirements : 
    String params = URLEncoder.encode("part1", "UTF-8") + "=" + URLEncoder.encode(part1, "UTF-8"); 
    params += "&" + URLEncoder.encode("part2", "UTF-8") + "=" + URLEncoder.encode(part2, "UTF-8"); 

//2. Connect to the website page : 
    URL url = new URL("http://www.website.com/page.php"); 
    URLConnection conn = (URLConnection) url.openConnection(); 
    conn.setConnectTimeout(20000); 
    conn.setDoOutput(true); 
    conn.setDoInput(true); 
    conn.connect(); 

//3. Call the page and send the parameters to it : 
    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream()); 
    out.write(params); 
    out.flush(); 
    out.close(); 

//4. Get the result : 
    Object contents = conn.getContent(); 
    InputStream is = (InputStream) contents; 
    StringBuffer buf = new StringBuffer(); 
    int c; 
    while((c = is.read()) != -1) { 
     buf.append((char) c); 
    } 
+0

他可以加載自己的Web瀏覽器的網址? – CanSpice 2011-04-07 20:22:57

+0

如果我是你,我會使用commons-httpclient – rodrigoap 2011-04-07 20:27:18

+0

我不確定你爲什麼在問題中明確提到「一家公司」。它是否在具有限制性代理/防火牆的公司網絡中?它與其他100多位用戶是否屬於同一公司? – BalusC 2011-04-07 20:47:39

回答

2

你確定它不是真實失敗的PHP腳本?

+5

我想我可以做出這個評論... – 2011-04-07 20:23:33

+0

我想你可以做出這個評論:) ...是的,我相信。該腳本適用於所有其他用戶+該程序有其他腳本來連接到我的服務器,如檢查程序更新,或從程序報告問題。用戶嘗試了所有這些,並且都沒有工作......全部掛起。 – Brad 2011-04-07 21:12:12

-1

編碼所有PARAMS字符串是這樣的:

String params = "part1" + "=" + part1; 
params += "&" + "part2" + "=" + part2; 
params = URLEncoder.encode(params, "UTF-8") 
+0

這與他的問題有什麼關係? – CanSpice 2011-04-07 20:35:09

+2

他已經以正確的方式對它們進行了編碼。你不應該編碼'='和'&'。否則它將成爲一個大參數名稱。 – BalusC 2011-04-07 20:39:39

+0

謝謝。我不知道,我總是使用commons-httpclient。 – rodrigoap 2011-04-07 20:44:04