Sending HTTP POST Request In Java

后端 未结 8 1296
清歌不尽
清歌不尽 2020-11-21 11:17

lets assume this URL...

http://www.example.com/page.php?id=10            

(Here id needs to be sent in a POST request)

I want to se

8条回答
  •  臣服心动
    2020-11-21 11:46

    String rawData = "id=10";
    String type = "application/x-www-form-urlencoded";
    String encodedData = URLEncoder.encode( rawData, "UTF-8" ); 
    URL u = new URL("http://www.example.com/page.php");
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty( "Content-Type", type );
    conn.setRequestProperty( "Content-Length", String.valueOf(encodedData.length()));
    OutputStream os = conn.getOutputStream();
    os.write(encodedData.getBytes());
    

提交回复
热议问题