我正在与期望POST参数并期望Request body的Web服务进行通信.我已经确认可以使用我的REST控制台完成这样的POST请求,但是我无法使用Apache库在
Java中发出这样的请求.
在下面的代码中,我能够POST到Web服务,它正确地接收变量raw_body的内容.如果我取消注释两个注释行中的第一行,则Web服务会收到“fname”参数,但它不再接收POST的正文.
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
...
HttpClient httpClient = new HttpClient();
String urlStr = "http://localhost:8080/MyRestWebService/save";
PostMethod method = new PostMethod(urlStr);
String raw_body = "This is a very long string, much too long to be just another parameter";
RequestEntity re = new StringRequestEntity(raw_body, "text/xml", "UTF-16");
//method.addParameter("fname", "test.txt");
//httpClient.getParams().setParameter("fname", "test.txt");
method.setRequestEntity(re);
如何传输参数和正文?