我寻找使HTTP POST请求给定的原始数据,我有。我花了一段时间寻找解决方案,尝试了一把,我正在寻找的一点点帮助。什么我希望做的看起来像这样的PHP代码:

<?
$url="http://localhost:3000";
$postdata="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<hi></hi>";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$result = curl_exec($ch);
curl_close($ch);

echo($result);
?>

我尝试是这样的:

  private String setXmlPostHeader(Document doc, PostMethod postMethod) throws java.io.IOException, java.io.UnsupportedEncodingException,    
    javax.xml.transform.TransformerException
  {
     ByteArrayOutputStream xmlBytes = new ByteArrayOutputStream();
     XML.serialize( doc, xmlBytes );
     final byte[] ba = xmlBytes.toByteArray();
     String data = new String(ba, "utf-8");
     InputStreamRequestEntity re = new InputStreamRequestEntity(new ByteArrayInputStream(ba));
     postMethod.setRequestEntity(re);
     postMethod.setRequestHeader("Content-type", MediaType.XML.toString() + "; charset=UTF-8");
     return data;
  }

和然后执行的PostMethod,但是这仅仅是不包含数据的讯息。有谁看到什么问题,我做?我想弄清楚如何改变这种方式来提高其实际工作。谢谢!

-Ken

有帮助吗?

解决方案

就不是 java.net.URLConnection 类工作做得更好?

其他提示

它不看起来像你在呼唤:

    int result = httpclient.executeMethod(postMethod);  
    postMethod.releaseConnection();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top