加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice restful类型接口的调用实例

发布时间:2020-12-16 23:01:12 所属栏目:安全 来源:网络整理
导读:因为项目是采用maven搭建的,所以先把所需要的jar包定义进来,我是采用apache的httpComponents第三方技术!如下: ???????????? !-- httpcomponents --dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpcore/artifactId/dependencydependen

因为项目是采用maven搭建的,所以先把所需要的jar包定义进来,我是采用apache的httpComponents第三方技术!如下:


????????????<!-- httpcomponents -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
		</dependency>
		<dependency>
			<groupId>jaxen</groupId>
			<artifactId>jaxen</artifactId>
		</dependency>

其次就是写调用客户端(get方式):


????????@Test
	public void getOrderList() throws Exception {

		CloseableHttpClient httpClient = HttpClients.createDefault();

		HttpGet httpGet = new HttpGet("接口请求路径……");
?????httpGet.setHeader("Accept","application/json");//如果要返回json结果,则需要设置此请求头信息
CloseableHttpResponse httpResponse;
		try {
			httpResponse = httpClient.execute(httpGet);
			HttpEntity entity = httpResponse.getEntity();
			System.out.println(IOUtils.toString(entity.getContent()));
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

其次就是写调用客户端(POST方式):

导包都是一样,就是发送请求的实体不同 如下:


????????/* 获取用户留资信息接口:GetUserLeaveMsg */
	@Test
	public void GetUserLeaveMsg() throws Exception {

		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(URL + "?method=GetUserLeaveMsg");
????????????????
???????????????//POST传递方式?
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("date","2014-12-01"));// yyyy-MM-dd
		nvps.add(new BasicNameValuePair("last_leave_msg_id","0"));
		nvps.add(new BasicNameValuePair("CurPage","1"));
		nvps.add(new BasicNameValuePair("PageSize","50"));

		httpPost.setHeader("Accept","application/json");
		httpPost.setEntity(new UrlEncodedFormEntity(nvps));
		CloseableHttpResponse httpResponse;
		try {
			httpResponse = httpClient.execute(httpPost);
			HttpEntity entity = httpResponse.getEntity();
			System.out.println(IOUtils.toString(entity.getContent()));
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



还会有一种就是传Json的方式,我也一并罗列出来了供大家参考:


????????/**
	 * 新闻资讯类接口
	 * 
	 * @throws Exception
	 * @throws UnsupportedEncodingException
	 */
	@Test
	public void newsTest() throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost("接口请求url……");
		httpPost.addHeader("Authorization","Basic " + sign);
		httpPost.setHeader("Accept","application/json");
		httpPost.setHeader("Content-Type","application/json");

		CloseableHttpResponse httpResponse;
		try {
???????????????????? //通过alibaba的fastJson的jar包来转成json所需要的数据格式
			Map<String,Object> params = new HashMap<String,Object>();
			List<String> list = new ArrayList<String>();
			list.add("2348");
			list.add("163");
????????????????????????params.put("DealerId",66934);
			params.put("TypeId",1);
			params.put("SeriesIds",list);
			
			String data = JSON.toJSONString(params);
			System.out.println("data-->" + data);

			/* 构造请求数据 */
			StringEntity myEntity = new StringEntity(data,ContentType.APPLICATION_JSON);
			/* 设置请求体 */
			httpPost.setEntity(myEntity);
			httpResponse = httpClient.execute(httpPost);
			HttpEntity entity = httpResponse.getEntity();
			System.out.println(httpPost.getURI().toString());

			System.out.println(IOUtils.toString(entity.getContent()));
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
就先写到这里了,还有通过SOAP协议定义的接口就下回再写,写得比较仓促难免会出错,如发现错误或者需要共同讨论的地方,发送信息到QQ:2481942148,望能给大家带来一些帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读