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 8182309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:49:24+00:00 2026-06-07T00:49:24+00:00

I have something I am trying to achieve. I have a web application running

  • 0

I have something I am trying to achieve.

I have a web application running on my localhost on port 8080.

I have a HTTP Server running on localhost:9005.

I have a JSP form that passes info to a servlet java class, that in turn performs the HTTP post to the URL on the HTTP Server localhost:9010 with the data string.

What I need to do is perform the POST and GET as part of the same connection. I have them working as two separate calls, but not on the same connection. It needs to be the same connection, as I post data, the HTTP Server takes this data, processes it and outputs unique data to this URL. Therefore the GET needs to be part of the same connection as he POST.

Can anyone please help?

This is my Process Request java code:

    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import javax.servlet.*;
    import javax.servlet.http.*;


    import java.util.List; 
    import java.util.Map.Entry; 

    public class ProcessRequest {

        public void sendRequestToGateway() throws Throwable{

            String message = URLEncoder.encode("OP1387927", "UTF-8");    

            try {           
                URL url = new URL("http://localhost:9005/json");            
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();            
                connection.setDoOutput(true);            
                connection.setRequestMethod("POST");            
                OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());           
                writer.write("operator=" + message);       
                writer.close();   

                System.out.println("connection.getResponseCode() : " + connection.getResponseCode());
                System.out.println("connection.getResponseMessage()" + connection.getResponseMessage());

                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 

                    receiveMessageFromGateway();

                } else {                
                    // Server returned HTTP error code.            
                }   

                //receiveMessageFromGateway();

            } catch (MalformedURLException e) {            
                // ...        
            } catch (IOException e) {           
                    // ...        
            }
        }

        public void receiveMessageFromGateway()  throws Throwable { 


            HttpURLConnection client = null; 
            OutputStreamWriter wr = null; 
            BufferedReader rd = null; 
            StringBuilder sb = null; 
            String line = null; 

            try { 

              URL url = new URL("http://localhost:9005/json"); 
              client = (HttpURLConnection) url.openConnection(); 
              client.setRequestMethod("GET"); 
              client.setDoOutput(true); 
              client.setReadTimeout(10000); 

              client.connect(); 
              System.out.println(" *** headers ***"); 
              for (Entry<String, List<String>> headernew : client.getHeaderFields().entrySet()) { 
                System.out.println(headernew.getKey() + "=" + headernew.getValue()); 
              } 

              System.out.println(" \n\n*** Body ***"); 
              rd = new BufferedReader(new InputStreamReader(client.getInputStream())); 
              sb = new StringBuilder(); 

              while ((line = rd.readLine()) != null) { 
                sb.append(line + '\n'); 
              } 

              System.out.println("body=" + sb.toString()); 

            } finally { 
              client.disconnect(); 
              rd = null; 
              sb = null; 
              wr = null; 
            } 
         }  

    }
  • 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-06-07T00:49:25+00:00Added an answer on June 7, 2026 at 12:49 am

    In general you can’t control connection reuse with HttpUrlConnection. You might be able to cast your connection to the specific implementation class and interfere with it but that’s a horribly unstable way of doing it.

    Apache HttpClient is probably a better option, given the requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to achieve something but i dont really know how I have set
I am trying to achieve something that should be easy in theory I have
All i have something i have been trying to do for a while and
I am just trying to find out that does puppet have something similar to
I think I have got something simple wrong here. I am trying to pass
I want to have one callback function after actions are done, I'm trying something
I have just found something very weird while developing a website. While trying to
I have searched a lot trying to find something for my purpose, however most
I'm trying to multithread something and i have my program set up such that
I have an HTML form that I need to post to the server with

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.