Java - sending HTTP parameters via POST method easily

前端 未结 17 1769
借酒劲吻你
借酒劲吻你 2020-11-21 05:54

I am successfully using this code to send HTTP requests with some parameters via GET method

void sendRequest(String request)
{
            


        
17条回答
  •  不思量自难忘°
    2020-11-21 06:46

    Appears that you also have to callconnection.getOutputStream() "at least once" (as well as setDoOutput(true)) for it to treat it as a POST.

    So the minimum required code is:

        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        //connection.setRequestMethod("POST"); this doesn't seem to do anything at all..so not useful
        connection.setDoOutput(true); // set it to POST...not enough by itself however, also need the getOutputStream call...
        connection.connect();
        connection.getOutputStream().close(); 
    

    You can even use "GET" style parameters in the urlString, surprisingly. Though that might confuse things.

    You can also use NameValuePair apparently.

提交回复
热议问题