JAVA 调用外部接口

系统需要调用guo wai一个接口,由于调用的guowai接口,需要通过proxy请求
现在有3个方案
1.使用proxy进行http请求
2.直接使用guo wai服务器部署项目
3.使用HK服务器作为中介获取返回数据

1:问题,代理不稳定
2:问题,相对于较繁琐

推荐用3

当不想每次写完代码,部署到HK服务器进行测试,可以先本地使用proxy进行测试,或者使用proxy IP。
工具都有提供端口号,具体可以咨询软件提供商

如需要用SoapUI,Postman测试,请看Utils内的
HTTP Auth认证请求- SoapUI
HTTP Auth认证请求- Postman
方法3:
相关JAR包
Maven依赖:

org.apache.httpcomponents
httpclient
4.5

org.jsoup jsoup 1.3.3

JAVA工具类:
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Encoder;

import java.io.IOException;

/**

  • Created by BugRoot on 2020/7/28.
    */
    public class HttpUtils {

    /**

    • Post请求webservice 带http认证

    • @param url 请求地址

    • @param param 请求参数

    • @param SOAPAction SOAPAction 接口 – 如不需要注释掉

    • @return
      */
      public static String httpClientPostSOAPActionAndAuth(String url, String param, String username,String password, String SOAPAction){
      System.out.println(param);
      CloseableHttpClient http = HttpClientBuilder.create().setProxy(new HttpHost(“127.0.0.1”,1081)).build();// 测试环境–本地proxy方式
      // CloseableHttpClient http = HttpClientBuilder.create().build();// 生产环境–部署到服务器
      HttpPost httpPost = new HttpPost(url);
      CloseableHttpResponse res = null;
      String entity = “”;
      // 请求错误
      String errorMessage="";
      try {
      // 下述方式不推荐,BASE64Encoder和BASE64Decoder这两个方法是sun公司的内部方法,没有 在java api中公开过, 使用这些方法是不安全的
      // String token = username +":"+ password;
      // BASE64Encoder base64en = new BASE64Encoder();
      // token = base64en.encode(token.getBytes(“utf-8”));
      // token = "Basic " + token;
      // System.out.println(token);
      // 建议使用apache的API-可引用import org.apache.commons.codec.binary.Base64;进行替换
      我使用的是java.util的
      String token = “Basic " + “Basic " + Base64.getEncoder().encodeToString(username +”:”+password.getBytes(StandardCharsets.UTF_8));
      StringEntity s = new StringEntity(param, “UTF-8”);
      //s.setContentEncoding(“UTF-8”);
      // s.setContentType(“text/xml”);//发送json数据需要设置contentType
      // s.setContentType(“application/xml”);//发送json数据需要设置contentType
      httpPost.setHeader(“Accept-Encoding”,“gzip,deflate”);
      httpPost.setHeader(“Content-Type”,“text/xml”);
      httpPost.setHeader(“SOAPAction”,SOAPAction); //设置接口方法名
      httpPost.setHeader(“Host”,“xxx.com”);// 如不需请注释
      httpPost.setHeader(“Connection”,“Keep-Alive”);
      httpPost.setHeader(“Accept-Encoding”,“gzip,deflate”);
      httpPost.setHeader(“User-Agent”,“Apache-HttpClient/4.1.1 (java 1.5)”);
      // httpPost.setHeader(“Authorization”,token);
      httpPost.setHeader(“Authorization”,“Basic ZmYxOWY0ZTBmZDhmNDE0NTllNjI0M2Q5ZTZmYmJkMTY6dUs3Ln14cCg=”);
      httpPost.setEntity(s);
      res = http.execute(httpPost);
      HttpEntity responseEntity = res.getEntity();
      System.out.println(“response status” + res.getStatusLine());
      // 获取状态码
      int statusCod = res.getStatusLine().getStatusCode();
      if(statusCod > 200){
      // TODO: 2020/7/28
      }
      // 解析响应报文 TODO
      if (responseEntity != null) {
      System.out.println(“response length:” + responseEntity.getContentLength());
      entity = EntityUtils.toString(responseEntity);
      System.out.println(“response content:” + entity);
      } else {
      errorMessage = “service exception”;
      }
      } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      } finally {
      try {
      http.close();
      res.close();
      } catch (IOException e) {
      e.printStackTrace();
      }

      }

      return entity;
      }
      }

如图一报文:

解析WebService返回的报文
JAVA 调用外部接口_第1张图片

String result = httpClientPostSOAPActionAndAuth(url, param, username, password, SOAPAction);
Document doc = Jsoup.parse(result );
Elements error = doc.getElementsByTag(“Error”);
if (error != null && error.size() > 0) {
String code = doc.getElementsByTag(“Code”).text();
String description = doc.getElementsByTag(“Description”).text();
System.out.println(code);
System.out.println(description);
message = code + description;
}

System.out.println(message );

你可能感兴趣的:(Java,java)