php能想远程提交xml文件,java怎么实现呀,请高手指教
- PHP code
<?php function postUrl($url, $postvar) { $ch = curl_init(); $headers = array( "POST ".$url." HTTP/1.0", "Content-type: text/xml; charset=\"gb2312\"", "Accept: text/xml", "Content-length: ".strlen($postvar) ); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $postvar); $res = curl_exec ($ch); curl_close ($ch); return $res; }$baiduXML = "<?xml version=\"1.0\" encoding=\"gb2312\"?> <methodCall> <methodName>extende</methodName> <params> <param><value><string>name</string></value></param> <param><value><string>我是测试</string></value></param> </methodCall>"; $res = postUrl('http://www.text.com', $baiduXML);?>
谁能把上面的php代码实现的功能
换成java的呀
[解决办法]
URL url = new URL("http://www.text.com");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeOut(10000);
conn.setDoInput(true);
conn.setDoOutPut(true);
conn.setRequestProperty("Content-Type","text/xml");
conn.setRequestProperty("Content-Length",$baiduXML.getBytes().length+"");
conn.getOutputStream().write($baiduXML.getBytes());
if(conn.getResponseCode()==200)
{
//成功返回做自己的事
}
[解决办法]
HttpURLConnection 就是这个