title: Sending several HTTP Requests in java - problem with OuputStreamWriter?
body: <p>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 :</p>
  
  <pre><code>try {
  String data =  "&lt;soap:Envelope xmlns:soap= ... datas xml ... &lt;/soap:Envelope&gt;";
  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());}
  </code></pre>
  
  <p>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 :</p>
  
  <blockquote>
    <p>access denied (java.net.SocketPermission ....url..... connect,resolve)</p>
  </blockquote>
  
  <p>How could I fix it ? I figure out it is possible to send several requests...
  Is the method disconnect() useful here ?</p>
  
  <p>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..</p>
  
  <p>Thank you very much for any advice or help !</p>
  
  <p>===================== EDIT =======================</p>
  
  <p>Here is a whole applet :</p>
  
  <pre><code>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);
  }
  }
  </code></pre>
  
  <p>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"...</p>
  
  <p>I hope it is clear.</p>
  
  <p>Thanks for any help or advice</p>