教你从头开始用JAVA创建一个自己的简单API并实现第三方调用

  public class HttpClientUtil {

  public static String doPost(String pathUrl, String data){

  OutputStreamWriter out = null;

  BufferedReader br = null;

  String result = "";

  try {

  URL url = new URL(pathUrl);

  //打开和url之间的连接

  HttpURLConnection conn = (HttpURLConnection) url.openConnection();

  //请求方法为"POST"

  conn.setRequestMethod("POST");

  // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在http正文内,因此需要设为true, 默认情况下是false;

  conn.setDoOutput(true);

  // Post请求不能使用缓存

  conn.setUseCaches(false);

  //设置请求属性

  conn.setRequestProperty("accept", "*/*");

  conn.setRequestProperty("connection", "Keep-Alive"); //维持长链接

  conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");

  conn.connect();

  //调用,获取输出流

  out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");

  //发送请求参数

  out.write(data);

  out.flush();

  //获取URLConnection对象对应的输入流

  InputStream is = conn.getInputStream();

  //构造一个字符流缓存

  br = new BufferedReader(new InputStreamReader(is));

  String str = "";

  while ((str = br.readLine()) != null){

  result += str;

  }

  System.out.println(result);

  //关闭流

  is.close();

  //断开连接,disconnect是在底层tcp socket链接空闲时才切断,如果正在被其他线程使用就不切断。

  conn.disconnect();

  } catch (Exception e) {

  e.printStackTrace();

  }

  return result;

  }

  }