我使用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」之前...
我希望它很清楚。
感謝任何幫助或建議
您是否關閉並重新打開連接? –
你是什麼意思?由disconnect()方法?我嘗試過,沒有它。我不知道如何關閉連接,除了使用.. ..? –
你使用小程序或類似的? –