web项目 调用外部接口 例子

论坛 期权论坛 脚本     
匿名技术用户   2020-12-22 14:16   59   0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class dtservice {

 public static void main(String[] args) throws Exception  {
  String requestUrl = "http://******/**/service/um/third/***.do";
  HttpPost http = new HttpPost(requestUrl);
  JSONObject caInfoJson = new JSONObject();
  
    try {
   BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
   CloseableHttpClient httpclient = HttpClients.custom()
     .setDefaultCredentialsProvider(credsProvider)
     .build();
   ;
   
   caInfoJson.put("OLD_ZSWYBS", "*****"); //接口参数
   
   NameValuePair pair1 = new BasicNameValuePair("jsonStr",
     caInfoJson.toString());
   List list = new ArrayList<>();
   list.add(pair1);
   UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
     list, "utf-8");
   http.setEntity(entity);
   String reslutDeptUser = printRestData(httpclient, http);
   
   System.out.println(reslutDeptUser);//reslutDeptUser 返回结果
  } catch (Exception e) {
   
   e.printStackTrace();
  }
 }

 /**
  * post请求。
  * 
  * @param httpclient
  * @param http
  * @return reslut 回文。
  * @throws
  * @throws Exception
  */
 public static String printRestData(CloseableHttpClient httpclient,
   HttpPost http) throws Exception {
  CloseableHttpResponse response = null;
  
  InputStream in = null;
  InputStreamReader rd = null;
  BufferedReader br = null;
  String reslut = "";
  response = httpclient.execute(http);
  HttpEntity entity = response.getEntity();
  in = entity.getContent();
  rd = new InputStreamReader(in, "UTF-8");
  br = new BufferedReader(rd);
  String line = br.readLine();
  if (line != null) {
   while (true) {
    reslut = reslut.concat(line);
    line = br.readLine();
    if (line == null) {
     break;
    }
   }
  }
  // reslut = unicodeToUtf8(reslut);
  if (entity != null) {
   System.out.println("Response content length: "
     + entity.getContentLength());
  }
  http.abort();

  try {
   if (response != null) {
    response.close();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  try {
   if (in != null) {
    in.close();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  try {
   if (rd != null) {
    rd.close();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  try {
   if (br != null) {
    br.close();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
  try {
   httpclient.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return reslut;
 }
}
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP