java后台请求resuful、soap接口总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yushenzaishi/article/details/82147416

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,这两步做好就可以了。

猜你喜欢

转载自blog.csdn.net/yushenzaishi/article/details/82147416