POST xml data using java

后端 未结 3 792
独厮守ぢ
独厮守ぢ 2021-02-14 21:04

I have used the following java code to POST xml data to a remote url and get the response. Here, I am using an xml file as the input. What I need is to pass the xml as a string

3条回答
  •  离开以前
    2021-02-14 21:50

    The setRequestEntity method on org.apache.commons.httpclient.methods.PostMethod has an overloaded version that accepts StringRequestEntity as argument. You should use this if you wish to pass in your data as a string (as opposed to an input stream). So your code would look something like this:

    String xml = "whatever.your.xml.is.here";
    PostMethod post = new PostMethod(strURL);     
    try {
        StringRequestEntity requestEntity = new StringRequestEntity(xml);
        post.setRequestEntity(requestEntity);
    ....
    

    Hope that helps.

提交回复
热议问题