HttpClient远程调用接口

本文深入探讨了GET和POST请求的区别与应用场景,详细解析了如何使用Java进行GET和POST请求,包括参数传递、响应处理等核心步骤,并介绍了从使用varchar到text类型的数据存储优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  详细参考这个博文: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来存储

 

转载于:https://www.cnblogs.com/xiaohouzai/p/8980498.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值