我正在尝试将数据上传到位于我正在使用的免费网络服务器上的数据库中,该服务器位于Hosting website。尝试发出发布请求时,我收到一条错误400消息。我已经在本地进行了测试,并且可以在xamp服务器上正常运行。
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
public class Dbtest {
public static void main(String[] args) throws Exception {
URL url = new URL("Testsite.test.php");
Map<String,Object> params = new LinkedHashMap<>();
params.put("Username", "Freddie the Fish");
params.put("Kills", 121);
params.put("Deaths", 10394);
params.put("Gold", 233);
StringBuilder postData = new StringBuilder();
for (Map.Entry<String,Object> param : params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);
}
}
有人对我可能做错的事情有任何建议吗?这是我收到的Java错误消息。
Server: nginx
Date: Mon, 11 Mar 2019 23:57:15 GMT
Content-Type: text/html
Content-Length: 166
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
Process finished with exit code 0