Java – use org. Org apache. HTTP sends an HTTP post request with a soap operation
•
Java
I'm using org apache. The HTTP API writes a hard - coded HTTP post request using soap operations
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();
}
}
}
Solution
Soapaction must be passed as an HTTP header parameter - when used, it is not part of HTTP body / payload
Here is an example of Apache httpclient: http://svn.apache.org/repos/asf/httpcomponents/oac.hc3x/trunk/src/examples/PostSOAP.java
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
