day3
IpTest.java
package com.bwf.apitest.day03; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.testng.annotations.Test; import java.io.IOException; import java.io.UnsupportedEncodingException; public class IpTest { @Test public void testIp() throws IOException { //用HttpClient进行soap协议的接口测试 CloseableHttpClient client = HttpClients.createDefault(); String uri = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx"; HttpPost post = new HttpPost(uri); String payload = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" + " <soap12:Body>\n" + " <getCountryCityByIp xmlns=\"http://WebXml.com.cn/\">\n" + " <theIpAddress>12.154.0.78</theIpAddress>\n" + " </getCountryCityByIp>\n" + " </soap12:Body>\n" + "</soap12:Envelope>"; //5.把字符串转成消息体 StringEntity entity = new StringEntity(payload); post.setHeader("Content-Type","application/soap+xml"); post.setEntity(entity); CloseableHttpResponse response = client.execute(post); String responseText = EntityUtils.toString(response.getEntity()); System.out.println(responseText); } }
MobileCodeTest.java
package com.bwf.apitest.day03; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.testng.annotations.Test; import java.io.IOException; public class MobileCodeTest { @Test public void testMobile() throws IOException { //1.创建HttpClient CloseableHttpClient client = HttpClients.createDefault(); //2.构建网址 String uri = "http://ws.webxml.com.cn/webservices/MobileCodeWS.asmx"; //3.创建请求 HttpPost post = new HttpPost(uri); //4.声明字符串保存请求参数 String paload = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:web=\"http://WebXml.com.cn/\">\n" + " <soap:Header/>\n" + " <soap:Body>\n" + " <web:getMobileCodeInfo>\n" + " <!--Optional:-->\n" + " <web:mobileCode>13681477136</web:mobileCode>\n" + " <!--Optional:-->\n" + " <web:userID></web:userID>\n" + " </web:getMobileCodeInfo>\n" + " </soap:Body>\n" + "</soap:Envelope>"; //5.把String转成StringEntity StringEntity entity = new StringEntity(paload,"UTF-8"); //6.在信息头中把消息体设置为soap+xml的类型 post.setHeader("Content-Type","application/soap+xml;charset=UTF-8;action=\"http://WebXml.com.cn/getMobileCodeInfo\""); //7.为请求设置消息体 post.setEntity(entity); //8.执行请求,获取响应 CloseableHttpResponse response = client.execute(post); //9.检查响应结果是否正确 String responseText = EntityUtils.toString(response.getEntity()); System.out.println(responseText); } }
浙公网安备 33010602011771号