使用 org.apache.http 发送带有 SOAP 操作的 HTTP Post 请求
2022-09-02 10:14:43
我正在尝试使用org.apache.http api编写一个带有SOAP操作的硬编码HTTP Post请求。我的问题是我没有找到添加请求正文的方法(在我的情况下 - SOAP操作)。我会很高兴得到一些指导。
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.RequestWrapper;
import org.apache.http.protocol.HTTP;
public class HTTPRequest
{
@SuppressWarnings("unused")
public HTTPRequest()
{
try {
HttpClient httpclient = new DefaultHttpClient();
String body="DataDataData";
String bodyLength=new Integer(body.length()).toString();
System.out.println(bodyLength);
// StringEntity stringEntity=new StringEntity(body);
URI uri=new URI("SOMEURL?Param1=1234&Param2=abcd");
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("Test", "Test_Value");
// httpPost.setEntity(stringEntity);
StringEntity entity = new StringEntity(body, "text/xml",HTTP.DEFAULT_CONTENT_CHARSET);
httpPost.setEntity(entity);
RequestWrapper requestWrapper=new RequestWrapper(httpPost);
requestWrapper.setMethod("POST");
requestWrapper.setHeader("LuckyNumber", "77");
requestWrapper.removeHeaders("Host");
requestWrapper.setHeader("Host", "GOD_IS_A_DJ");
// requestWrapper.setHeader("Content-Length",bodyLength);
HttpResponse response = httpclient.execute(requestWrapper);
} catch (Exception e) {
e.printStackTrace();
}
}
}