Java 使用org.apache.HTTP发送带有SOAP操作的HTTP Post请求
我正在尝试使用org.apache.httpapi编写一个带有SOAP操作的硬编码HTTP Post请求。 我的问题是我没有找到添加请求主体的方法(在我的例子中是SOAP操作)。 我很乐意得到一些指导Java 使用org.apache.HTTP发送带有SOAP操作的HTTP Post请求,java,apache,http,post,httpwebrequest,Java,Apache,Http,Post,Httpwebrequest,我正在尝试使用org.apache.httpapi编写一个带有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; i
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();
}
}
}
soapAction必须作为http头参数传递-使用时,它不是http正文/负载的一部分 在这里查看apache httpclient的示例:
。。。正在使用org.apache.httpapi代码>
您需要在请求中包含SOAPAction
作为标题。由于有httpPost
和requestWrapper
句柄,有三种方法可以添加头
1. httpPost.addHeader( "SOAPAction", strReferenceToSoapActionValue );
2. httpPost.setHeader( "SOAPAction", strReferenceToSoapActionValue );
3. requestWrapper.setHeader( "SOAPAction", strReferenceToSoapActionValue );
唯一的区别是,addHeader
允许多个具有相同头名称的值,setHeader
仅允许唯一头名称setHeader(…
over写入同名的第一个头
您可以根据自己的需要使用其中任何一个。这是一个完整的工作示例:
import org.apache.http.HttpEntity;
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.util.EntityUtils;
public void callWebService(String soapAction, String soapEnvBody) throws IOException {
// Create a StringEntity for the SOAP XML.
String body ="<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://example.com/v1.0/Records\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><SOAP-ENV:Body>"+soapEnvBody+"</SOAP-ENV:Body></SOAP-ENV:Envelope>";
StringEntity stringEntity = new StringEntity(body, "UTF-8");
stringEntity.setChunked(true);
// Request parameters and other properties.
HttpPost httpPost = new HttpPost("http://example.com?soapservice");
httpPost.setEntity(stringEntity);
httpPost.addHeader("Accept", "text/xml");
httpPost.addHeader("SOAPAction", soapAction);
// Execute and get the response.
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String strResponse = null;
if (entity != null) {
strResponse = EntityUtils.toString(entity);
}
}
import org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.util.EntityUtils;
public void callWebService(字符串soapAction、字符串soapEnvBody)引发IOException{
//为SOAP XML创建StringEntity。
字符串体=”“+soapEnvBody+”;
StringEntity StringEntity=新的StringEntity(主体,“UTF-8”);
stringEntity.setChunked(true);
//请求参数和其他属性。
HttpPost HttpPost=新的HttpPost(“http://example.com?soapservice");
httpPost.setEntity(stringEntity);
addHeader(“Accept”、“text/xml”);
addHeader(“SOAPAction”,SOAPAction);
//执行并获得响应。
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse response=httpClient.execute(httpPost);
HttpEntity=response.getEntity();
字符串strResponse=null;
如果(实体!=null){
strResponse=EntityUtils.toString(实体);
}
}
当通过java客户端调用WCF服务时,确定需要在soap操作上设置什么的最简单方法是加载wsdl,转到与服务匹配的操作名称。从那里选择操作URI并将其设置在soap操作头中。完成了
例如:来自wsdl
<wsdl:operation name="MyOperation">
<wsdl:input wsaw:Action="http://tempuri.org/IMyService/MyOperation" message="tns:IMyService_MyOperation_InputMessage" />
<wsdl:output wsaw:Action="http://tempuri.org/IMyService/MyServiceResponse" message="tns:IMyService_MyOperation_OutputMessage" />
它给出了415Http响应代码作为错误
所以我补充说
httppost.addHeader("Content-Type", "text/xml; charset=utf-8");
现在一切都好了,Http:200这是我尝试过的一个例子,它对我有效:
创建XML文件SoapRequestFile.XML
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetConversionRate>
<!--Optional:-->
<tem:CurrencyFrom>USD</tem:CurrencyFrom>
<!--Optional:-->
<tem:CurrencyTo>INR</tem:CurrencyTo>
<tem:RateDate>2018-12-07</tem:RateDate>
</tem:GetConversionRate>
</soapenv:Body>
</soapenv:Envelope>
你的密码在哪里written@Ravinder你能看看我朋友的问题吗?你能看看我朋友的问题吗?你能解释一下为什么添加这个会导致错误吗?我正在尝试你的代码并得到这个异常:java.net.SocketException:Software-caused-connection-abort:recv-failed
。我有htt在我的类路径中有pclient-4.5.2.jar和httpcore-4.4.4.jar。知道吗?
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:GetConversionRate>
<!--Optional:-->
<tem:CurrencyFrom>USD</tem:CurrencyFrom>
<!--Optional:-->
<tem:CurrencyTo>INR</tem:CurrencyTo>
<tem:RateDate>2018-12-07</tem:RateDate>
</tem:GetConversionRate>
</soapenv:Body>
</soapenv:Envelope>
import java.io.File;
import java.io.FileInputStream;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Assert;
import org.testng.annotations.Test;
import io.restassured.path.json.JsonPath;
import io.restassured.path.xml.XmlPath;
@Test
public void getMethod() throws Exception {
//wsdl file :http://currencyconverter.kowabunga.net/converter.asmx?wsdl
File soapRequestFile = new File(".\\SOAPRequest\\SoapRequestFile.xml");
CloseableHttpClient client = HttpClients.createDefault(); //create client
HttpPost request = new HttpPost("http://currencyconverter.kowabunga.net/converter.asmx"); //Create the request
request.addHeader("Content-Type", "text/xml"); //adding header
request.setEntity(new InputStreamEntity(new FileInputStream(soapRequestFile)));
CloseableHttpResponse response = client.execute(request);//Execute the command
int statusCode=response.getStatusLine().getStatusCode();//Get the status code and assert
System.out.println("Status code: " +statusCode );
Assert.assertEquals(200, statusCode);
String responseString = EntityUtils.toString(response.getEntity(),"UTF-8");//Getting the Response body
System.out.println(responseString);
XmlPath jsXpath= new XmlPath(responseString);//Converting string into xml path to assert
String rate=jsXpath.getString("GetConversionRateResult");
System.out.println("rate returned is: " + rate);
}