Server returned HTTP response code: 400

Posted on 2006-07-11
Last Modified: 2008-01-09
I'm trying to access a URL in my program. I opened the URL page in my web browser and it looks fine but when I run my program. I got an IOException that says: "Server returned HTTP response code: 400"

I'm not sure why I got this since it seems that I could open it in my web browser (Internet Explorer).

This is what I do in my code:
BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("http://myPage.htm").openStream()));

Any advise?

Question by:rnicholus
  • 3
  • 3

Accepted Solution

astorer earned 200 total points
ID: 17086279
http://mypage.html is not a valid URL.  Are you opening a file on local disk or one from a web server?

Make sure that the URL in the IE navigation bar is copied exactly into your code.  I'd expect something like one of the following:


Author Comment

ID: 17086333
Hi astorer,

Thanks for your reply. I used http://mypage.html just as an example.
I have found the solution that the URL actually have spaces which automatically converted to %20 by the browser but not by the java program.

I have requested this question to be removed.

Expert Comment

ID: 17086714
Hi rnicholus!
use URLEncoder.encode to encode a URL same as:
 public static String forURL(String aURLFragment){
    String result = null;
    try {
      result = URLEncoder.encode(aURLFragment, "UTF-8");
    catch (UnsupportedEncodingException ex){
      throw new RuntimeException("UTF-8 not supported", ex);
    return result;
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 17095141
Dear moderator: I have provided the solution that I found for this problem in my post on 07/11/2006 05:40PM CDT.

Expert Comment

ID: 17097465
I'm not sure how the system works here.

People have been trying to help with solutions to the problem.  The reason that experts could not find the actual problems was that the question was not posed correctly.

Specifically,  the URL quoted was "http://myPage.htm".  This is clearly a bad URL and would have caused the problem defined.  In effect, I answered the question posed.  To refuse the points because the question was badly posed seems a little uncharitable and against the spirit of what Experts Exchange is all about.


Author Comment

ID: 17100787
Dear astorer:
I think you're right that I should have made the questions more clear at the very beginning, i.e: mentioning that the URL quoted was just an example.
I'm sorry.

Thanks for all the help

Expert Comment

ID: 17101767
Awwwww, thanks.  I feel bad now!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question