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

  • Home
  • SEARCH
  • 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 1012005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:52:48+00:00 2026-05-16T09:52:48+00:00

I was working on HTTP post using java and encountered a weird stream behavior.

  • 0

I was working on HTTP post using java and encountered a weird stream behavior. Here’s what happened:

Func() {
    String data = “MyMessage”
    URL url = new URL("http://edsall:8080"); 
    URLConnection conn = url.openConnection(); 
    conn.setDoOutput(true); 

    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    wr.write(data); 
    wr.flush();         

    // Get the response 
    String line; 
    while ((line = rd.readLine()) != null) { 
    } 

    wr.close();
    rd.close();
}

Request recvd by the server:
POST / HTTP/1.1
User-Agent: Java/1.6.0_20
Host: edsall:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-type: application/x-www-form-urlencoded
Content-Length: 0

Observe that the content length is always 0. Initially, I couldn’t figure out what the problem was. Finally, the following re-arrangement of code did the trick:

Func() {
    …
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
    wr.write(data); 
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    …
}

I am not able to understand this clearly. How does opening a handle to the input stream affect the output stream?

  • 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-16T09:52:49+00:00Added an answer on May 16, 2026 at 9:52 am

    Opening the input stream (or querying the status) “commits” the request—i.e., the request is sent to the server.

    I’m not sure what happened to the data that you wrote to the output stream (I would have hoped for an exception if it didn’t actually get sent), but the content-length header is sent (with a length of zero) when getInputStream() is called.

    The entire request must be sent before accessing any part of the response. This is normal URLConnection behavior.

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

Sidebar

Related Questions

I'm using a Java program to POST some XML data from a remote client
I having an issue from my HTTP Post. The code I'm using are working
I'm working on http://pizzli.com/darrenwp/?p=107 . When I try and set the post background height
I'm working on a wordpress site, with a blog post that ends like: http://www.blog.com/?p=2
here is the working fiddle: http://jsfiddle.net/2bNsC/67/ I need to place the 3 items (text
Here is the web page I'm working on: http://sebastianbiermanlytle.com/games.htm . The flashplayer is at
Here's the code I'm currently working with: http://jsfiddle.net/gEMCm/ I'm trying to make an image
I'm making an HTTP POST request using curl like this: curl -v -i -d
I want to make http form post using NSURLConnection in iOS. I have two
I just try to post data to google by using the following code,but always

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.