简体   繁体   English

Java-当我尝试获取源代码服务器时返回的HTTP响应代码:400

[英]Java - when I tried to get the source code server returned HTTP response code: 400

My issue is in my title,also I will give java code and detail. 我的问题在标题中,我还将提供Java代码和详细信息。

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 : 例如,我通过html a href =“ ...”解析发送“http://ekenlermangalkomuru.com/urunlerimiz/liste/144/BinarKömürü”作为参数,但错误报告是:

java.io.IOException: Server returned HTTP response code: 400 for URL: http://ekenlermangalkomuru.com/urunlerimiz/liste/144/BinarKömürü java.io.IOException:服务器返回HTTP响应代码:400,用于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) 在sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源)在java.net.URL.openStream(未知来源)

The link works on all browsers but when I tried to connect via java code it doesnt work.How I can I solve ? 该链接在所有浏览器上均有效,但是当我尝试通过Java代码进行连接时却不起作用。我该如何解决? Thanks for all advices.. 感谢您的所有建议。

Error 400 means that the request is malformed so that the server is unable to process it. 错误400表示请求格式错误,因此服务器无法处理它。 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. 对于类似以下内容: http ://ekenlermangalkomuru.com/urunlerimiz/liste/144 /BinarKömürü,至少ö和ü不是ASCII字母,需要进行编码以创建格式正确的请求。

Error 400 means that you didn't make a "good" request to the site. 错误400表示您未向该站点发出“良好”请求。 I think you would need something more than just a BufferedReader to open a URL via HTTP. 我认为您不仅仅需要BufferedReader才能通过HTTP打开URL。 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? 这是因为该协议需要知道您要查看的内容以及是否有可用的缓存等。要打开URL,应使用HttpUrlConnection,请选中“ 如何在Java中发送HTTP请求”? out for info, also http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html could be of help. 请参阅http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html以获得帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM