如何从 URL 获取字符串而不省略 JAVA 中的空格

How to GET a string from a URL without omitting spaces in JAVA

我有一个发送 GET 请求并获取字符串的简单程序。 URL 是我编写的 PHP 页面,它从服务器上的数据库中获取一些数据并将其解析为带有一堆白色-space 字符(我需要)的字符串。 问题是 space 在对 java 应用程序的响应中被省略了,我的问题是如何避免省略它们?

我的java代码:

URL servicesUrlObj = new URL("https://myurl.mysite.com/placeholder.php");
HttpURLConnection connection = (HttpURLConnection) servicesUrlObj.openConnection();

connection.setRequestMethod("GET");

int responseCode = connection.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + servicesUrlObj);
System.out.println("Response Code : " + responseCode);

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();

while ((inputLine = reader.readLine()) != null) {
    content.append(inputLine);
}
reader.close();

System.out.println(content.toString());

原始字符串:

200329951 123 3 03/09/18

从请求返回的字符串:

200329951123303/09/18

这不是一个非常优雅的解决方案,但您可以考虑在 while 循环中添加一个 content.append(" "),或者甚至只是将当前行变成 content.append(inputLine + " ");

感谢 Manu Rivas 的评论,我找到了问题所在。我没有正确地将空格附加到 php 文件中的输出参数。 我做了:

$spaces . ' ';

而不是:

$spaces = $spaces . ' ';