Import Java.io.ByteArrayInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.util.Iterator;
Import Java.util.Map;
Import org.apache.http.HttpEntity;
Import Org.apache.http.HttpResponse;
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.testng.annotations.DataProvider;
Import Org.testng.annotations.Test;
public class Httpclienttest {
Using Excel data to drive data, TestNG's dataprovider annotations provide data very handy, the implementation principle does not have to delve into, it will automatically call Exceldataprovider and return a map to the test method, If you are using Dataprovider Excel for Data driven, you should specify the parameters of the test method as map
@DataProvider (name = "DataPro")
Public iterator<object[]> Data () {
return new Exceldataprovider ("Soaptest", "Testsoap");
}
@Test (Dataprovider = "DataPro")
public void HttpPost (map<string, string> data) throws IOException {
Encrypt the transmitted data, which is encrypted using the SHA-1 algorithm
Soapkey Soapkey = new Soapkey ();
String key = Soapkey.getmessagedigest (Data.get ("Data"), "SHA-1");
Replace the "<" and ">" of the request XML principal data with "<" and ">"
String strdata = new String (Data.get ("Data"). Replace ("<", "<")). Replace (">", ">");
The requested XML data
String soapreuqest = "<soapenv:envelope xmlns:soapenv=\" http://schemas.xmlsoap.org/soap/envelope/\ "xmlns:ser=\" Http://service.ws.gpo.yy.com/\ ">"
+ "<soapenv:Header/>" + "<soapenv:Body><ser:sendRecv4Supplier><!--Optional:--><suser > "
+ data.get ("user") + "</sUser><!--optional:--><spwd>" + data.get ("passwd")
+ "</sPwd><!--optional:--><sjgbm>" + data.get ("JGBM")
+ "</sJgbm><!--optional:--><sversion>1.0.0.0</sversion><!--optional:--><sxxlx > "+ data.get (" Msgtype ")
+ "</sXxlx><!--optional:--><ssign>" + key + "</sSign><!--optional:-->" + "<xmldata > "+ strdata
+ "</xmlData>" + "</ser:sendRecv4Supplier></soapenv:Body></soapenv:Envelope>";
1. Create a httpclient client
Closeablehttpclient httpclient = Httpclients.createdefault ();
2. Get the HTTP Post
HttpPost HttpPost = new HttpPost (Data.get ("Urlstr"));
3. Set the character set encoding of the sending request, if not specified, it will be sent by default in the 16 character set encoding, and the returned response will be garbled.
Httppost.addheader ("Content-type", "application/x-www-form-urlencoded; charset= "+" Utf-8 ");
4. Adding SOAP request data to the HTTP POST method
byte[] by = soapreuqest.getbytes ("Utf-8");
InputStream InputStream = new Bytearrayinputstream (by, 0, by.length);
Inputstreamentity reqentity = new Inputstreamentity (InputStream, by.length);
Httppost.setentity (reqentity);
5. Performing an HTTP POST request
HttpResponse response = Httpclient.execute (HttpPost);
6. Get the status code returned by the server
int statuscode = Response.getstatusline (). Getstatuscode ();
7. Get the return entity of the server
httpentity entity = response.getentity ();
8. Convert the entity returned by the server into a string (request response result)
String responsemsg = entityutils.tostring (entity);
System.out.println ("interface:" + data.get ("Msgtype") + ": Returns the status code with the response result:" + statuscode + ":" + responsemsg);
}
}
Test the WebService Interface (SOAP) with the HTTP interface using the HttpClient tool class, test the WebService interface here, and test the HTTP interface implementation in a similar way