在不调用getInputStream()的情况下发送Java POST请求

时间:2022-08-22 19:30:13

I would like to send a POST request in Java. At the moment, I do it like this:

我想用Java发送POST请求。目前,我是这样做的:

URL url = new URL("myurl");
URLConnection con = url.openConnection();
con.setDoOutput(true);
PrintStream ps = new PrintStream(con.getOutputStream());
ps.println("key=" + URLEncoder.encode("value"));
// we have to get the input stream in order to actually send the request
con.getInputStream();  
ps.close();

I do not understand why I have to call con.getInputStream() in order to actually send the request. If I do not call it, the request is not sent.

我不明白为什么我必须调用con.getInputStream()才能实际发送请求。如果我不打电话,则不会发送请求。

Is there a problem using PrintStream? It should not matter if I take a PrintStream, PrintWriter or something else, right?

使用PrintStream有问题吗?如果我使用PrintStream,PrintWriter或其他东西,这没关系,对吧?

3 个解决方案

#1


0  

URL represents some source.

URL代表一些来源。

URLConnection represents a connection to the resource.

URLConnection表示与资源的连接。

you need call connection.connect() to connect the connection

你需要调用connection.connect()来连接连接

#2


0  

Try to add

尝试添加

con.setDoInput (false);
before writing to the output.

P.S.: and you should also call con.connect () as swanliu says.

P.S。:你也应该像swanliu所说的那样打电话给con.connect()。

Update
This is what I came up with

更新这是我想出的


    private static final void sendPostRequest (final String urlAddress, String key, String value) throws Exception
    {
        URL url = new URL (urlAddress);
        URLConnection con = url.openConnection ();
        con.setDoOutput (true);
        con.setDoInput (false);
        PrintStream ps = new PrintStream (con.getOutputStream ());
        ps.println (key + "=" + URLEncoder.encode (value, "UTF-8"));
        ps.flush ();
        con.connect ();
        ps.close ();
    }

I checked with WireShark that a tcp connection is being established and that closed. But don't know how check that the server received the request. If you have a quick way to check it you may try that code.

我检查了WireShark是否正在建立tcp连接并关闭。但是不知道如何检查服务器是否收到了请求。如果您有快速检查方法,可以尝试使用该代码。

#3


0  

I think a post of another thread answered my question. Sorry, but I found it too late. You can find it here.

我认为另一个帖子的帖子回答了我的问题。对不起,但我发现为时已晚。你可以在这里找到它。

PS: Unfortunately, * added my last answer to the question as a comment, because my answer was too short. And it is not possible to mark a comment as the correct answer... Hope this one is long enough :-)

PS:不幸的是,*将我对问题的最后答案添加为评论,因为我的答案太短了。并且不可能将评论标记为正确答案...希望这个评论足够长:-)

#1


0  

URL represents some source.

URL代表一些来源。

URLConnection represents a connection to the resource.

URLConnection表示与资源的连接。

you need call connection.connect() to connect the connection

你需要调用connection.connect()来连接连接

#2


0  

Try to add

尝试添加

con.setDoInput (false);
before writing to the output.

P.S.: and you should also call con.connect () as swanliu says.

P.S。:你也应该像swanliu所说的那样打电话给con.connect()。

Update
This is what I came up with

更新这是我想出的


    private static final void sendPostRequest (final String urlAddress, String key, String value) throws Exception
    {
        URL url = new URL (urlAddress);
        URLConnection con = url.openConnection ();
        con.setDoOutput (true);
        con.setDoInput (false);
        PrintStream ps = new PrintStream (con.getOutputStream ());
        ps.println (key + "=" + URLEncoder.encode (value, "UTF-8"));
        ps.flush ();
        con.connect ();
        ps.close ();
    }

I checked with WireShark that a tcp connection is being established and that closed. But don't know how check that the server received the request. If you have a quick way to check it you may try that code.

我检查了WireShark是否正在建立tcp连接并关闭。但是不知道如何检查服务器是否收到了请求。如果您有快速检查方法,可以尝试使用该代码。

#3


0  

I think a post of another thread answered my question. Sorry, but I found it too late. You can find it here.

我认为另一个帖子的帖子回答了我的问题。对不起,但我发现为时已晚。你可以在这里找到它。

PS: Unfortunately, * added my last answer to the question as a comment, because my answer was too short. And it is not possible to mark a comment as the correct answer... Hope this one is long enough :-)

PS:不幸的是,*将我对问题的最后答案添加为评论,因为我的答案太短了。并且不可能将评论标记为正确答案...希望这个评论足够长:-)