Java httpserver 在收到 POST 请求时崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28325241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java httpserver crashes upon receiving POST requests
提问by Alex Brashear
I am trying to write a simple HTTP server in Java that can handle POST requests. While my server successfully receives the GET, it crashes on the POST.
我正在尝试用 Java 编写一个可以处理 POST 请求的简单 HTTP 服务器。当我的服务器成功接收到 GET 时,它在 POST 时崩溃了。
Here is the server
这是服务器
public class RequestHandler {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
server.createContext("/requests", new MyHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class MyHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
String response = "hello world";
t.sendResponseHeaders(200, response.length());
System.out.println(response);
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
}
And here is the Java code I use to send the POST
这是我用来发送 POST 的 Java 代码
// HTTP POST request
private void sendPost() throws Exception {
String url = "http://localhost:8080/requests";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
Each time the POST request crashes on this line
每次 POST 请求在这一行崩溃
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
but when I change the URL to the one provided in the example where I found this it works.
但是当我将 URL 更改为示例中提供的 URL 时,我发现它可以工作。
采纳答案by RealSkeptic
Instead of
代替
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
Use
用
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
You are connecting to a URL which is not HTTPS. When you call obj.openConnection()
, it decides whether the connection is HTTP or HTTPS, and returns the appropriate object. When it's http
, it won't return an HttpsURLConnection
, so you cannot convert to it.
您正在连接到非 HTTPS 的 URL。当您调用 时obj.openConnection()
,它会决定连接是 HTTP 还是 HTTPS,并返回相应的对象。当它是 时http
,它不会返回HttpsURLConnection
,因此您无法转换为它。
However, since HttpsURLconnection
extends HttpURLConnection
, using HttpURLConnection
will work for both http
and https
URLs. The methods that you are calling in your code all exist int the HttpURLConnection
class.
但是,由于HttpsURLconnection
extends HttpURLConnection
, usingHttpURLConnection
对http
和https
URL 都有效。您在代码中调用的方法都存在于HttpURLConnection
类中。