0

My issue is in my title,also I will give java code and detail.

void getSourceCode(String text_url){

    String source_code="";
    BufferedReader reader = null;

    try {
          reader = new BufferedReader(new InputStreamReader((new URL(text_url)).openStream(), Charset.forName("UTF-8")));
          String inputLine;
          while ((inputLine = reader.readLine()) != null) {
            source_code+=inputLine.replace(" ", "");
          }
    } 

    catch (Exception e) {
        e.printStackTrace();
    } 

    finally {
          if (reader != null) {
                try {
                  reader.close();
                } 
                catch (IOException e) {
                  e.printStackTrace();
                }
          }
    }

    System.out.println( source_code );
}

For example,I send "http://ekenlermangalkomuru.com/urunlerimiz/liste/144/BinarKömürü" as the parameter via html a href="..." parsing but the error report is :

java.io.IOException: Server returned HTTP response code: 400 for URL: http://ekenlermangalkomuru.com/urunlerimiz/liste/144/BinarKömürü
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at java.net.URL.openStream(Unknown Source)

The link works on all browsers but when I tried to connect via java code it doesnt work.How I can I solve ? Thanks for all advices..

Sedat KURT
  • 428
  • 3
  • 8
  • 20

2 Answers2

2

Error 400 means that the request is malformed so that the server is unable to process it. Are you sure that you have properly URL encoded the request URL? For something like: http://ekenlermangalkomuru.com/urunlerimiz/liste/144/BinarKömürü, at least ö and ü are not ASCII letters and need to be encoded to create a well-formed request.

Mathias Schwarz
  • 6,833
  • 18
  • 27
  • what does 'escaped' mean ? Can you give an example.By the way.I have a java program that gets source code of given url.After that it parses source code for links and for these links it calls all processes recursively.So getSourceCode() -> parseSourceCodeForLinks() -> getSourceCodeOfTheseLinks() -> ParseSourceCode......... I mean I dont give parameter to getSourceCode(String text_url) manually, it gives what parser parse from source code programmatically – Sedat KURT Sep 18 '12 at 08:56
  • Does something like `http://ekenlermangalkomuru.com/urunlerimiz/liste/144/BinarK%C3%B6m%C3%BCr%C3%BC` work better? – Mathias Schwarz Sep 18 '12 at 09:26
  • `"http://ekenlermangalkomuru.com/urunlerimiz/liste/144/" + URLEncoder.encode("BinarKömürü")` will do that for you... – Mathias Schwarz Sep 18 '12 at 09:29
  • Yet it is working now.So you say that wrong part is turkish characters lik ö,ü...., Then I must send whole link to character filter method and this method must turn letters which is not in ASCII tables to legal characters with URLEncoder .Right? – Sedat KURT Sep 19 '12 at 07:27
  • Yes, you need to encode the characters. Encoding the whole link won't make sense though: You don't want e.g. `:` and `/` to be encoded... – Mathias Schwarz Sep 19 '12 at 09:03
1

Error 400 means that you didn't make a "good" request to the site. I think you would need something more than just a BufferedReader to open a URL via HTTP. That's because the protocol needs to know what you want to see and if you have a cache available etc. To open a URL you should use HttpUrlConnection, check How to send HTTP request in java? out for info, also http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html could be of help.

Community
  • 1
  • 1
lfxgroove
  • 3,430
  • 2
  • 20
  • 32