在POST请求中,将参数作为请求的正文发送到标头之后。
要使用
HttpURLConnection进行POST,你需要在打开连接后将参数写入连接。
这段代码可以帮助你入门:
String urlParameters = "param1=a¶m2=b¶m3=c";byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 );int postDataLength = postData.length;String request = "http://example.com/index.php";URL url = new URL( request );HttpURLConnection conn= (HttpURLConnection) url.openConnection();conn.setDoOutput( true );conn.setInstanceFollowRedirects( false );conn.setRequestMethod( "POST" );conn.setRequestProperty( "Content-Type", "application/x-www-form-urlenpred");conn.setRequestProperty( "charset", "utf-8");conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));conn.setUseCaches( false );try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) { wr.write( postData );}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)