get和post转发接口开发

1.前端想做一个功能,填写表单后,给企业微信传微信群名称以及用户列表,直接拉起一个群组。

我这边帮忙做一个转发。

 /**
   *GET工具接口
   **/

  public Result TranferGet(String url) {
    try {
      url = java.net.URLDecoder.decode(url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    //1.获得一个httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();
    //2.生成一个get请求
    HttpGet httpget = new HttpGet(url);
    CloseableHttpResponse response = null;
    try {
      //3.执行get请求并返回结果
      response = httpclient.execute(httpget);
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    String result = null;
    try {
      //4.处理结果,这里将结果返回为字符串
      HttpEntity entity = response.getEntity();
      if (entity != null) {
        result = EntityUtils.toString(entity);
      }
    } catch (Exception e) {
      e.printStackTrace();
      LoggerEx.error("转发失败。");
    } finally {
      try {
        response.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return ok(Json.toJson(result));
  }

  /**
   *POST工具接口
   **/

  public Result TranferPost2(String url) {
    try {
      url = java.net.URLDecoder.decode(url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    String outStr = request().body().asJson().toString();
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new StringEntity(outStr, "UTF-8"));
    String result = null;
    try {
      HttpResponse response = httpClient.execute(httpPost);
      result = EntityUtils.toString(response.getEntity(),
        "UTF-8");
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return ok(Json.toJson(result));
  }

Get请求整体是比较顺利的,唯一踩的坑是本地通过postman请求ok,但是联调过程中发现有问题,url未进行decode转码导致的。

post请求遇到了很多坑:

(1)最开始查了一下网上的资料,设想自己要拿到的是一个url,然后将body中的值转化成map。后来觉得太复杂了。

选择直接去取request请求的body(),然后转化成String。

(2)最开始是使用

String outStr = request().body().asJson().toString();

然后使用postman自己调试的时候:

发现一直报错,空指针异常。

因为没有注意到postman的方式是用的text。

后来瞎试,发现使用

String outStr = request().body().asText();是可以的。

就采用了这种方式。

(3)然后调试通了,给前端用,发现又报错空指针异常,把所有东西打印出来,发现outStr为空。

排查后发现前段使用的传递方式为form-data。

这样是取不到值的。

后来和前端协调使用json的形式来传参数,后段写法:String outStr = request().body().asJson().toString();

使用postman调试成功,和前端联调成功。

被嫌弃写的不好。

嗯,重写了个工具类,把这部分封装了一下:

package util;

import io.netty.channel.ConnectTimeoutException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import system.log.LoggerEx;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class HttpTool {

  public static String doTransmit(String url, String type) {
    try {
      url = java.net.URLDecoder.decode(url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpRequestBase httpRequest;
    if ("GET".equals(type)) {
      httpRequest = new HttpGet(url);
    } else {
      httpRequest = new HttpPost(url);
      ((HttpPost) httpRequest).setEntity(new StringEntity(type, "UTF-8"));
    }
    String result = null;
    CloseableHttpResponse response = null;
    try {
      httpRequest.setConfig(RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build());
      response = httpClient.execute(httpRequest);
      result = EntityUtils.toString(response.getEntity(),
        "UTF-8");
    } catch (ClientProtocolException e) {
      e.printStackTrace();
      LoggerEx.error("发送请求出现异常。" + e);
    } catch (ConnectTimeoutException e) {
      e.printStackTrace();
      return "Connection time out";
    } catch (IOException e) {
      e.printStackTrace();
      LoggerEx.error("发送请求出现异常。" + e);
    } finally {
      try {
        response.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return result;
  }
}

使用:

 public Result doGet(String url) {
    return ok(Json.toJson(HttpTool.doTransmit(url, "GET")));
  }

  public Result doPost(String url) {
    String outStr = request().body().asJson().toString();
    return ok(Json.toJson(HttpTool.doTransmit(url, outStr)));
  }

猜你喜欢

转载自blog.csdn.net/third_/article/details/81568565
今日推荐