• Post Reply Bookmark Topic Watch Topic
  • New Topic

Server returned HTTP response code: 405 for URL  RSS feed

 
Jiri Nejedly
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spend several minutes every day visting internet stores of used DVDs if there's something I would like to buy.
I happenned to me, that to save time, I should write simple application which will download the list of goods automatically. The application is based on simple code (the same for every site):

URL url = new URL("http://...");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.flush();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

From the read result I extract the info which interests me. It seems OK, but one adress which is OK in Internet Explorer gives me this error on the last line above:

Server returned HTTP response code: 405 for URL: ...

What's wrong ? Some workaround would do.
Thanks.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the server is expecting more information in the request headers.
 
Jiri Nejedly
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What could it be? The URL address is the same as copied from IE (where it works).
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
405 means "method not allowed". Are you using GET when POST is required, or vice-versa?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
405 is "method not allowed". You're using "doSetOutput(true)" which forces HttpURLConnection to use POST, not GET, and so I'm guessing the site is prohibiting POST requests. The very simple solution is to remove the call to doSetOutput(true) -- you don't need it for any reason!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using "doSetOutput(true)" which forces HttpURLConnection to use POST, not GET,

Yow! Where and how did you find this out?
The URLConnection setDoOutput() javadocs don't mention it. I thought you had to use the HttpURLConnection setRequestMethod("POST") call to force use of POST.


Bill
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!