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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:28:09+00:00 2026-05-25T19:28:09+00:00

I have a design question. I’m using the HttpURLConnection class to navigate a website.

  • 0

I have a design question.

I’m using the HttpURLConnection class to navigate a website. In order to obtain an HttpURLConnection instance, I have a local getter method. This is responsible for setting all the request properties, including possible cookies. Anyway, throughout the life cycle of this application, I use multiple HttpURLConnection instances. And never is there more than one open connection.

Anyway, I have another local method for sending requests in the form of an HTTP POST. Here is the code:

private final InputStream sendRequest(final HttpURLConnection conn, final String content){
    InputStream in = null;
    DataOutputStream out = null;
    try{
        // Write data out to the stream
        out = new DataOutputStream(conn.getOutputStream()); 
        out.writeBytes(content);
        out.flush();

        // Get the input stream
        in = conn.getInputStream();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    finally{
        try
        {
            if(out != null){
                out.close();
            }
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }

    return in;
}

As you can see, as soon as I’m done writing out the content, I open an input stream on the connection and return it. The reason I do this is if I do not open the input stream on the connection, the application will hang. Therefore, I’m leaving it up to the caller as to whether or not they want to parse the incoming content. Also, the caller needs the connection instance because I leave it up to them to disconnect (that is, close the connection).

So, I guess my question is, is this a poor design? That is, opening the input stream of the connection object that the caller already has an instance of and then returning it?

  • 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-25T19:28:10+00:00Added an answer on May 25, 2026 at 7:28 pm

    It’s a shame you’re not allowed to use Apache HttpClient, but a little OOP may help you untangle your code.

    Introduce a ResponseHandler interface for the caller to implement. You might also want to provide some out-of-the-box implementations in public static fields (if thread-safe) or via factory methods for the common cases like “give me a String” (make sure to consult the “charset” part of the response’s Content-Encoding header when creating your InputStreamReader) and “give me a byte[]”.

    Don’t supply just the InputStream to the ResponseHandler as you might need access to the response headers for character encoding, etc.

    Again, this would really be much much nicer with HttpClient :/

    (the following code lacks basic sanity checks for the parameters, please add them)

    public interface ResponseHandler<T> {
        T handleResponse(HttpURLConnection conn);
    }
    
    public <T> T sendRequest(final HttpURLConnection conn, final String content,
            final ResponseHandler<T> handler)
            throws IOException {
        OutputStream out = null;
        try {
            out = conn.getOutputStream();
            // uses platform encoding, might want to explicitly
            // specify "UTF-8"
            out.write(content.getBytes());
            out.flush();
    
            return handler.handleResponse(conn);
        } finally {
            // closing out omitted for brevity
        }
    }
    

    Edit: If you can somehow managed to get rid of HttpUrlConnection in the calling code, that would be even better. Check out if you can get along with a String for the URL and a Map<String, Object> for the parameters, maybe wrap this in a HttpRequest object.

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

Sidebar

Related Questions

Just wanted opinions on a design question. If you have a C++ class than
I have a design question... I want to create my own image class called
This is more of a design question. Suppose you have a method like this
I have a design question. I have a website where users enter short messages
More of a design/conceptual question. At work the decision was made to have our
I have a question about design winforms. Should I use, or not, group boxes
I have posted a question about multilanguage database design here, [] What are best
An idle question on language design, see Does C# have a right hand if
I have a pretty simple question which perhaps someone familiar with Server/Client design &
I have a simple question. I am trying to design a simple Android app,

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.