1. Request restful interface
The request restful interface is divided into two types, depending on the type of interface request parameters published by the other side.
First, the parameter is of String type, which can be directly requested by httpClient post;
String url = "http://127.0.0.1:8480/jkcsYsd/test"; HttpClient httpclient = new HttpClient(); // Sending interface PostMethod post = new PostMethod(url); post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8"); post.addParameter("id", "228457"); httpclient.executeMethod(post); String info = new String(post.getResponseBody(), "utf-8"); System.out.println(info);
The second one is json type, which adopts encapsulation URLConnection;
String url = "http://127.0.0.1:8480/jkcsYsd/queryFwbByUserInfo"; String data ="{\"userid\":\"YLJG_5bcd1318-3074-11e7-bfe3-00163e0e1cc6\"}"; URL weburl = new URL(url); URLConnection connection = weburl.openConnection(); connection.addRequestProperty("content-type", "application/json"); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection .getOutputStream(),"utf-8"); out.write(data); out.flush(); out.close(); String sCurrentLine; String sTotalString; sCurrentLine = ""; sTotalString = ""; InputStream l_urlStream; l_urlStream = connection.getInputStream(); BufferedReader l_reader = new BufferedReader(new InputStreamReader( l_urlStream, "utf-8")); while ((sCurrentLine = l_reader.readLine()) != null) { sTotalString += sCurrentLine; } System.out.println(sTotalString);
Connection. Addrequestproperty ("content type", "application/json"). Please note that the content type is set to application/json;
Parameters are in json format: {\ "userid \": \ "yljg ﹣ 5bcd1318-3074-11e7-bfe3-00163e0e1cc6 \"};
2:soap interface
The soap interface adopts xml file format transmission, which can generate client code directly using wsdl2java or post request.
I won't talk about generating client code here. I'll talk about using post request method.
The most important points of using post request are as follows:
1. Complete message
2. Content type setting of the request
String url = "http://localhost:9999/zhgl_dq/Zlzb?wsdl"; String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:zl=\"http://zl.portal.wonders.com/\">"; xml = xml +" <soapenv:Header/><soapenv:Body><zl:getZlzb><arg0>YHF_0016</arg0><arg1></arg1> </zl:getZlzb></soapenv:Body></soapenv:Envelope>"; URL weburl = new URL(url); URLConnection connection = weburl.openConnection(); connection.addRequestProperty("content-type", "text/xml"); connection.setDoOutput(true); OutputStreamWriter out = new OutputStreamWriter(connection .getOutputStream(),"UTF-8"); out.write(xml); out.flush(); out.close(); String sCurrentLine; String sTotalString; sCurrentLine = ""; sTotalString = ""; InputStream l_urlStream; l_urlStream = connection.getInputStream(); BufferedReader l_reader = new BufferedReader(new InputStreamReader( l_urlStream, "UTF-8")); while ((sCurrentLine = l_reader.readLine()) != null) { sTotalString += sCurrentLine; } System.out.println(sTotalString);
In fact, it is basically the same as the above restful json request, but the format of the encapsulated parameters is different. Because the data transmission format of soap is xml, it is necessary to encapsulate the complete soap request message and set the content type to application/json. These two steps can be done well.