HttpClient远程调用接口
详细参考这个博文:http://www.cnblogs.com/itliucheng/p/5065619.html
一、get请求:
//关键代码就这几行
String urlNameString ="http://127.0.0.1:8601/setResult/" + text + "/" + newWordsText; // //往服务器端写内容 也就是发起http请求需要带的参数
// // 根据地址获取请求
HttpGet request = new HttpGet(urlNameString);//这里发送get请求
// // 获取当前客户端对象
HttpClient httpClient = new DefaultHttpClient();
// // 通过请求对象获取响应对象
HttpResponse response = httpClient.execute(request);
======================================================================
@RequestMapping("getWeChatUserInfo")
@ResponseBody
public String getWeChatUserInfo(String token,String openid){
String urlNameString = "https://api.weixin.qq.com/sns/userinfo?access_token=TOKEN&openid=OPENID";
urlNameString=urlNameString.replace("TOKEN", token);
urlNameString=urlNameString.replace("OPENID",openid);
String result="";
try {
// 根据地址获取请求
HttpGet request = new HttpGet(urlNameString);//这里发送get请求
// 获取当前客户端对象
HttpClient httpClient = new DefaultHttpClient();
// 通过请求对象获取响应对象
HttpResponse response = httpClient.execute(request);
// 判断网络连接状态码是否正常(0--200都数正常)
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result= EntityUtils.toString(response.getEntity(),"utf-8");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
//....result是用户信息,站内业务以及具体的json转换这里也不写了...
}
//要调用的接口形式
@GetMapping("/setResult/{word}/{classifyName}")
public Result<String> setResult(@PathVariable("word") String word, @PathVariable("classifyName") String classifyName) {
int i1 = classifyService.selectClassifyCountByName(classifyName);
二、但是由于get方式请求内容大小有限,现在改由post请求提交:
//接口形式
// @ApiOperation("测试选择数据并添加分类接口,word需要分类的文本,classifyName添加分类")
@PostMapping("/setResult")
public Result<String> setResult(@RequestParam String word,@RequestParam String classifyName) {
int i1 = classifyService.selectClassifyCountByName(classifyName);
if (i1 > 0) {
//分类已存在
Classify classify = classifyService.selectClassifyByName(classifyName);
KeyWord keyword = new KeyWord();
keyword.setKeywordName(word);
keyword.setClassifyId(classify.getId());
classifyService.addKeyWord(keyword);//添加KeyWord数据
modelExercise();//更新本地硬盘的表
//关键代码就这几行
String urlNameString = "http://127.0.0.1:8601/setResult"; //POST的URL
HttpPost httppost = new HttpPost(urlNameString);
//建立HttpPost对象
List<NameValuePair> params = new ArrayList<NameValuePair>();
//建立一个NameValuePair数组,用于存储欲传送的参数
params.add(new BasicNameValuePair("word", word));
params.add(new BasicNameValuePair("classifyName", classifyName));
//添加参数
httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//设置编码
HttpResponse response = new DefaultHttpClient().execute(httppost);
//发送Post,并返回一个HttpResponse对象
if (response.getStatusLine().getStatusCode() == 200) {//如果状态码为200,就是正常返回
String result = EntityUtils.toString(response.getEntity());
}
三、结果数据库用varchar无法容纳内容,现在改由text来存储
HttpClient远程调用接口的更多相关文章
- HttpClient方式调用接口的实例
使用HttpClient的方式调用接口的实例. public class TestHttpClient { public static void main(String[] args) { // 请求 ...
- java Webservice(一)HttpClient远程调用
我们将Web Service发布在Tomcat或者其他应用服务器上后,有很多方法可以调用该Web Service,常用的有两种: 1.通过浏览器HTTP调用,返回规范的XML文件内容 2.通 ...
- 几种远程调用接口协议简单比较和web service(SOAP)与HTTP接口的区别:
什么是web service? 答:soap请求是HTTP POST的一个专用版本,遵循一种特殊的xml消息格式Content-type设置为: text/xml任何数据都可以xml化. ...
- HttpClient方式调用接口的java 简单案例源码+附jar包
1 package com.itNoob.httpClient; import org.apache.commons.httpclient.HttpClient; import org.apache. ...
- Httpclient远程调用WebService示例(Eclipse+httpclient)
package cn.com.taiji.pos.httpserver; import java.io.BufferedInputStream;import java.io.ByteArrayOutp ...
- Httpclient远程调用WebService示例
我们将Web Service发布在Tomcat或者其他应用服务器上后,有很多方法可以调用该Web Service,常用的有两种: 1.通过浏览器HTTP调用,返回规范的XML文件内容 2.通 ...
- tomcat+webservice实现简单的web服务远程调用接口
1,准备工作: ①需要使用到jaxws的一系列架包,网址:http://jax-ws.java.net,我下的是比较新的一个版本(下载好以后吧架包发在lib下),②webservice的一个工具 ...
- httpclient方式调用接口
public class ToInterface { /** * post方式提交表单(模拟用户登录请求) */ public static void postForm() { // 创建默认的htt ...
- SpringBoot使用HttpClient远程调用
一. Get请求 try { //拼接url url = url+"access_token="+token+"&department_id=1&fetc ...
随机推荐
- HDU 6237.A Simple Stone Game-欧拉函数找素因子 (2017中国大学生程序设计竞赛-哈尔滨站-重现赛)
A Simple Stone Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- Quaternion Euler
geometry_msgs::Quaternion orientation = map->info.origin.orientation; tf::Matrix3x3 mat(tf:: ...
- POJ 1741 Tree 树的分治
原题链接:http://poj.org/problem?id=1741 题意: 给你棵树,询问有多少点对,使得这条路径上的权值和小于K 题解: 就..大约就是树的分治 代码: #include< ...
- 【bzoj3524】【Poi2014】【Couriers】可持久化线段树(主席树)水题
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=62485671 向大(hei)佬(e)势力学(di ...
- POJ 1511 Invitation Cards(单源最短路,优先队列优化的Dijkstra)
Invitation Cards Time Limit: 8000MS Memory Limit: 262144K Total Submissions: 16178 Accepted: 526 ...
- Uprobes: userspace probes >= Linux 3.5
https://lwn.net/Articles/499190/ https://github.com/andreoli/fulltrace Prominent features in Linux 3 ...
- sqlserver 出现sql被锁时,查看加锁和被锁的sql
原文:sqlserver 出现sql被锁时,查看加锁和被锁的sql DECLARE @spid INT DECLARE @blk INT DECLARE @count INT DECLARE @ind ...
- Springboot默认加载application.yml原理以及扩展
Springboot默认加载application.yml原理以及扩展 SpringApplication.run(...)默认会加载classpath下的application.yml或applic ...
- powerdesigner操作
转:http://w-tingsheng.blog.163.com/blog/static/25056034201392331452977/ 使用PowerDesigner生成数据库建表SQL脚 本时 ...
- [置顶]
kubernetes资源类型--ingress
Ingress在K8S1.1之前还没有. 概念 Ingress是一种HTTP方式的路由转发机制,为K8S服务配置HTTP负载均衡器,通常会将服务暴露给K8S群集外的客户端. Ingress是一个允许入 ...