2011-07-01 47 views
0

我使用Java爲了發送HTTP請求到webservices。我設法發送一個,但我不能發送兩個。這是我的代碼的一部分(部分爲便於閱讀,刪除:在java中發送多個HTTP請求 - 與OuputStreamWriter問題?

try { 
String data = "<soap:Envelope xmlns:soap= ... datas xml ... </soap:Envelope>"; 
URL url = new URL(".......url........"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.addRequestProperty("Content-Type", "text/xml"); 
conn.addRequestProperty("SOAPAction", .......action......."); 
conn.setDoOutput(true); 
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); // fail here for the second request 

//write parameters 
writer.write(data); 
writer.flush(); 

// Get the response 
StringBuffer answer = new StringBuffer(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
String line; 
while ((line = reader.readLine()) != null) {answer.append(line); 
} 
    writer.close(); 
reader.close(); 

//Output the response 
String str=answer.toString(); 
// conn.disconnect(); // Should I put it ? 
label.setText(str); 
} 

catch (Exception ex) {label.setText(ex.getMessage());} 

這個請求工作正常,如果我把同樣的後做一些測試,我發現它不工作(我改的名字變量,例如..)。我發現它失敗在與OuputStreamWriter行。我得到這個錯誤與getMessage的異常:

訪問被拒絕(java.net.SocketPermission .... url .....連接,解決)

我該如何解決它?我figu退出它可以發送幾個請求... 方法disconnect()在這裏有用嗎?

我試圖通過其他類發送請求(其實,我有一個網頁和JavaScript調用請求一個接一個),它並沒有太多工作..

非常感謝您的任何建議或幫助!編輯=======================

這是一個整體的小程序:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import netscape.javascript.JSException; 
import netscape.javascript.JSObject; 
import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 

public class Test extends JApplet { 
private JSObject jso; 
private JLabel label = new JLabel(); 

public void init(){ 
    this.setSize(300, 80); 

    label.setHorizontalAlignment(JLabel.CENTER); 
    label.setForeground(Color.blue); 
    label.setText("hello world"); 

    this.getContentPane().add(label, BorderLayout.NORTH); 
} 

public void doJavascript(){ 
    label.setText("hellooooooooo"); 
} 


public void closeConnect(String SECTK, String SESSID){ 
    jso = JSObject.getWindow(this); 
    label.setHorizontalAlignment(JLabel.CENTER); 
    label.setForeground(Color.blue); 
    try{ 
    String data ="dataaaaa"; 
    label.setText("yes2"); 
URL url = new URL(".........url............."); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.addRequestProperty("Content-Type", "text/xml"); 
conn.addRequestProperty("SOAPAction", ".....url........"); 
conn.setDoOutput(true); 
label.setText("yes25"); 
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); 
label.setText("yes3"); 
writer.write(data); 
writer.flush(); 

StringBuffer answer = new StringBuffer(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
String line; 
while ((line = reader.readLine()) != null) { 
    answer.append(line); 
} 
writer.close(); 
reader.close(); 
//Output the response*/ 
    String str=answer.toString(); 
    conn.disconnect(); 
    label.setText(str); 
} 
catch (Exception ex) { 
// label.setText("nooo"); 
label.setText(ex.getMessage()); 
} 
this.getContentPane().add(label, BorderLayout.NORTH); 
} 
} 

如果我叫doJavascript從一個JavaScript文件,它的工作原理。如果我從相同的JavaScript文件調用closeConnect,它不起作用,並且我在我的標籤「yes25」中檢索,該標籤就在「OuputStreamWriter」之前...

我希望它很清楚。

感謝任何幫助或建議

+0

您是否關閉並重新打開連接? –

+0

你是什麼意思?由disconnect()方法?我嘗試過,沒有它。我不知道如何關閉連接,除了使用.. ..? –

+0

你使用小程序或類似的? –

回答

0

我有固定的是:有是從JavaScript到Java的一些種方法的調用安全問題 - 例如HTTP請求:http://jdk6.java.net/plugin2/liveconnect/#SECURITY_MODEL

因此,代碼必須卜改變這樣的:

final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

OutputStreamWriter writer; 
       // This is needed as code called from Javascript does not have the rights to do this 
       try 
       { 
        writer = 
         AccessController.doPrivileged(
          new PrivilegedExceptionAction<OutputStreamWriter>() { 
           public OutputStreamWriter run() throws IOException { 
            return new OutputStreamWriter(conn.getOutputStream()); 
           } 
          } 
         ); 
       } 
       catch (PrivilegedActionException e) { 
        // e.getException() should be an instance of IOException, 
        // as only "checked" exceptions will be "wrapped" in a 
        // PrivilegedActionException. 
        throw (IOException) e.getException(); 
       } 

StringBuffer answer = new StringBuffer(); 
       BufferedReader reader; 
       // This is needed as code called from Javascript does not have the rights to do this 
       try 
       { 
        reader = new BufferedReader(
         AccessController.doPrivileged(
          new PrivilegedExceptionAction<InputStreamReader>() { 
           public InputStreamReader run() throws IOException { 
            return new InputStreamReader(conn.getInputStream()); 
           } 
          } 
         ) 
        ); 
       } 
       catch (PrivilegedActionException e) { 
        // e.getException() should be an instance of IOException, 
        // as only "checked" exceptions will be "wrapped" in a 
        // PrivilegedActionException. 
        throw (IOException) e.getException(); 
       } 
0

是第二次要求去一個不同的服務器,其中小程序是從下載?如果是這樣,那就是問題所在。

Applets無法連接到或從任何第三方服務器(除源自它的服務器以外的任何服務器)檢索資源。

http://download.oracle.com/javase/tutorial/deployment/applet/security.html

我會建議使用RMI有小應用程序與服務器通信解決這一問題,並讓服務器將代表小程序的實際XML請求。這很容易做到,並且使用下載服務器作爲這樣的代理使小程序能夠執行安全管理器限制他們執行的各種操作。

+0

謝謝您的回答。第二個請求的目標是與第一個請求相同的服務器。如果我把這兩個請求放在一個applet(或兩個不同的applet)的init()中,它可以正常工作。但是,如果我在第一個applet或另一個applet中創建第二個類,則不起作用,就像我上面所描述的那樣。我不認爲這是你提出的問題 - 但我會盡力在init中做所有事情。 –

+0

爲了精確起見,我認爲我可以在init()中做所有事情,但如果我可以針對不同的請求使用不同的類,它會提供幫助,所以我仍在尋找。 –

+0

如果您編寫了一個全新的小程序來重現此問題,然後在此處發佈整個源代碼,它可能會有所幫助。 –