java.io.IOException:服务器返回HTTP响应代码:411

时间:2015-09-16 12:06:46

标签: java server httpurlconnection ioexception

我正在尝试将以下网址发布到服务器:

http://localhost:3000/webserver/query?q=AddData(123,2015)

使用下面的代码,服务器返回错误411。

HttpURLConnection connection = null;

try {
    //Create connection
    URL url = new URL(targetURL);

    connection = (HttpURLConnection)url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/xml");
    connection.setRequestProperty("Authorization", "Basic " + Base64.encode("administrator:")); 
    connection.setRequestProperty("Content-Length", "0");
    connection.setDoOutput(true);

    //Get Response  
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    String line;

    while((line = rd.readLine()) != null) {
      System.out.println(line);
    }

    rd.close();
  } 
  catch (Exception e) {
    e.printStackTrace();
  } 
  finally {
    if(connection != null) connection.disconnect(); 
  }

我知道错误411是无效的Content-Length方法。我试过给Content-Length提供URL的长度,但错误仍然存​​在。

你能帮忙解决这个问题吗?

0 个答案:

没有答案