笔记-RestTemplate,WebService
RestTemplate
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
public class RestTemplateTest {
public static void main(String[] args) {
String baseUrl = "http://localhost:8088/";
HttpHeaders headers = new HttpHeaders();
headers.set("contentType", "application/json;charset=UTF-8");
sendGet(baseUrl,headers);
}
public static void sendPost(String baseUrl, HttpHeaders headers) {
String requestUrl = baseUrl + "api/user/list";
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("pageNum","1");
paramsMap.put("pageSize","20");
paramsMap.put("role","用户");
HttpEntity<HashMap<String, String>> entity = new HttpEntity<>(paramsMap, headers);
RestTemplate template = new RestTemplate();
ResponseEntity<Map> exchange = template.exchange(requestUrl, HttpMethod.POST, entity, Map.class);
System.out.println("exchange.getBody()" + exchange.getBody());
}
public static void sendGet(String baseUrl, HttpHeaders headers) {
String requestUrl = baseUrl +"api/login/test?phone={phone}&password={password}";
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("phone","1361111111");
paramsMap.put("password","123456");
RestTemplate template = new RestTemplate();
ResponseEntity<String> exchange = template.getForEntity(requestUrl,String.class,paramsMap);
System.out.println("exchange.getBody()" + exchange.getBody());
}
private static void addAuth(HttpHeaders headers, String yourEncryptedWorlds) {
byte[] key = (yourEncryptedWorlds).getBytes();
String authHeader = new String(Base64.encodeBase64(key));
headers.set("Authorization", "Basic "+authHeader);
}
}
WebService
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class WebServiceTest {
public static void main(String[] args) throws IOException {
URL url = new URL("http://localhost:8080/webservice/user/getUersList?wsdl");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("content-type", "text/xml;charset=utf-8");
connection.setDoInput(true);
connection.setDoOutput(true);
String username = "";
String password = "";
String param = "";
String soapXML = getXML(param, username, password);
OutputStream os = connection.getOutputStream();
os.write(soapXML.getBytes());
int responseCode = connection.getResponseCode();
if (200 == responseCode) {
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while (null != (temp = br.readLine())) {
sb.append(temp);
}
System.out.println(sb.toString());
is.close();
isr.close();
br.close();
}
os.close();
}
public static String getXML(String param, String username, String password) {
String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://webservice.carmng.gacc.kmss.landray.com/\">"
+ "<soapenv:Header>" + "<tns:RequestSOAPHeader xmlns:tns=\"http://sys.webservice.client\">"
+ "<tns:user>" + username + "</tns:user>" + "<tns:password>" + password + "</tns:password>"
+ "</tns:RequestSOAPHeader>" + "</soapenv:Header>" + " <soapenv:Body>" + " <web:getUserList>" +param+ " </web:getUserList>" + "</soapenv:Body>" + "</soapenv:Envelope>";
return soapXML;
}
}