Java http Post请求

时间:2016-01-26 10:52:07

标签: java http

我如何在java中提出这种格式的请求:

POST URL http / 1.1

页眉: 主持人...... 接受... ......更多标题 ...... 请求正文 发送= .....一些数据

我用我的标题和正文编写了这段代码,但它没有用

String urlParameters  = "send = .....";
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-urlencoded"); 
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 个答案:

没有答案