1. 程式人生 > >java後臺請求resuful、soap介面總結

java後臺請求resuful、soap介面總結

1.請求restful介面

請求restful介面分為兩種,要看對方釋出的介面請求引數是什麼型別。

第一種:引數為String型別,直接採用httpClient post請求就可以了;

        String url = "http://127.0.0.1:8480/jkcsYsd/test";
		HttpClient httpclient = new HttpClient();
		// 傳送介面
		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);

第二種:引數是json 型別,採用封裝的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") 這裡要注意,將content-type設定為application/json;

引數為json格式:{\"userid\":\"YLJG_5bcd1318-3074-11e7-bfe3-00163e0e1cc6\"};

2:soap介面

soap介面採用的是xml檔案格式傳輸,可以採用wsdl2java直接生成客戶端程式碼,也可以用post請求。

生成客戶端程式碼這裡就不講了,講一下采用post請求方法。

採用post請求最重要的是兩點:

1.完整的報文

2.請求的content-type設定

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);

其實大體上和上面的restful json請求是一樣的,只是封裝的引數格式不一樣,由於soap採用的資料傳輸格式是xml,所以需將完整的soap請求報文封裝好,設定content-type為application/json,這兩步做好就可以了。