zoukankan      html  css  js  c++  java
  • [Java] 两种发起POST请求方法,并接收返回的响应内容的处理方式

    1、利用apache提供的commons-httpclient-3.0.jar包

    代码如下:

    /**
      * 利用HttpClient发起POST请求,并接收返回的响应内容
      * 
      * @param url 请求链接
      * @param type 交易或响应编号
      * @param message 请求内容
      * @return 响应内容
      */
      public String transRequest(String url, String type, String message) {
      // 响应内容
      String result = "";
      // 定义http客户端对象--httpClient
      HttpClient httpClient = new HttpClient();
      // 定义并实例化客户端链接对象-postMethod
      PostMethod postMethod = new PostMethod(url);
      try{
       // 设置http的头
       postMethod.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");
       // 填入各个表单域的值
       NameValuePair[] data = { new NameValuePair("type", type), new NameValuePair("message", message) };
       // 将表单的值放入postMethod中
       postMethod.setRequestBody(data);
       // 定义访问地址的链接状态
       int statusCode = 0;
       try
     
    { // 客户端请求url数据 statusCode = httpClient.executeMethod(postMethod); }
    catch (Exception e)
      { e.printStackTrace(); }
    // 请求成功状态-200 if (statusCode == HttpStatus.SC_OK) { try { result = postMethod.getResponseBodyAsString(); } catch (IOException e) { e.printStackTrace(); } } else { log.error("请求返回状态:" + statusCode); } } catch (Exception e) { log.error(e.getMessage(), e); } finally { // 释放链接 postMethod.releaseConnection(); httpClient.getHttpConnectionManager().closeIdleConnections(0); } return result; }

    2、利用java自带的java.net.*包下提供的工具类

    代码如下:

    /**
      * 利用URL发起POST请求,并接收返回信息
      * 
      * @param url 请求URL
      * @param message 请求参数
      * @return 响应内容
      */
     @Override
     public String transport(String url, String message)
    {   StringBuffer sb
    = new StringBuffer();   try
      {     URL urls = new URL(url);     HttpURLConnection uc = (HttpURLConnection) urls.openConnection();     uc.setRequestMethod("POST");     uc.setRequestProperty("content-type", "application/x-www-form-urlencoded");     uc.setRequestProperty("charset", "UTF-8");     uc.setDoOutput(true);     uc.setDoInput(true);     uc.setReadTimeout(10000);     uc.setConnectTimeout(10000);
        OutputStream os
    = uc.getOutputStream();     DataOutputStream dos = new DataOutputStream(os);     dos.write(message.getBytes("utf-8"));     dos.flush();     os.close();
        BufferedReader in
    = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8"));     String readLine = "";     while ((readLine = in.readLine()) != null)
        {
          sb.append(readLine);     }     in.close();     }
        catch (Exception e)
        {       log.error(e.getMessage(), e);     }
        return sb.toString(); }

    --------------------------------------

    欢迎您,进入 我系程序猿 的cnBlog博客。

    你不能改变你的过去,但你可以让你的未来变得更美好。一旦时间浪费了,生命就浪费了。

    You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.

    --------------------------------------

    分享到QQ空间  

  • 相关阅读:
    英文句子相似性判断
    机器学习(一)特征工程基本流程
    经典卷积神经网络(LeNet、AlexNet、VGG、GoogleNet、ResNet)的实现(MXNet版本)
    Google免费GPU使用教程(亲测可用)
    XGBoost、LightGBM的详细对比介绍
    终身机器学习:一种可持续学习的范式
    机器学习中安全与隐私问题(对抗性攻击)
    频繁模式挖掘中Apriori、FP-Growth和Eclat算法的实现和对比(Python实现)
    Webpack教程,更新中
    @babel/plugin-transform-runtime
  • 原文地址:https://www.cnblogs.com/jqmtony/p/3711006.html
Copyright © 2011-2022 走看看