I am using Java in order to send HTTP Requests to webservices. I manage to send one, but I cannot send two. This is a part of my code (some parts removed for readability :
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());}
This request works fine. If I put the same after and do some tests, I find that it does not work (I change the names of variables, in case..). I have found that it fails at the line with the OuputStreamWriter. I get this error with getMessage in the exception :
access denied (java.net.SocketPermission ….url….. connect,resolve)
How could I fix it ? I figure out it is possible to send several requests…
Is the method disconnect() useful here ?
I tried to send the request via an other class (in fact, I have a webpage and javascript call the requests one by one), and it does not work too..
Thank you very much for any advice or help !
===================== EDIT =======================
Here is a whole applet :
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);
}
}
If I call doJavascript from a javascript file, it works. If I call closeConnect from the same javascript file, it does not work, and I retrieve in my label “yes25”, which is just before the “OuputStreamWriter”…
I hope it is clear.
Thanks for any help or advice
I have fixed it : there is a security problem for calls from javascript to java for some kinds of methods – for example http requests : http://jdk6.java.net/plugin2/liveconnect/#SECURITY_MODEL
So the code must bu changed like this :
and
and