Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6130251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:50:11+00:00 2026-05-23T16:50:11+00:00

I am using Java in order to send HTTP Requests to webservices. I manage

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:50:12+00:00Added an answer on May 23, 2026 at 4:50 pm

    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 :

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

    and

    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();
                    }
    

    and

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to send data to servlet HttpConnection c = (HttpConnection)Connector.open(http://localhost:8585/resposweb/resposweb?action=create_order;deviceside=true);
Since I'm using Java 1.4.2, this means that I cannot use Java's implementation of
I'm using GSSAPI in Java in order to login to an LDAP server using
I am using Java 1.4 with Log4J. Some of my code involves serializing and
i know virtually no java but i'm trying to learn some for this project.
I am using Jackson in order to send data in JSON type between a
I am using java to create an application for network management. In this application
I'm making a Java console application that needs to send an HTTP request to
Using Java, how can I test that a URL is contactable, and returns a
Using java, minus the exception handling, it is as simple as FileOutputStream ostream =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.