zoukankan      html  css  js  c++  java
  • httpclient之发送字符串

    public String HttpPostWithJson(String url, String json) {
            String returnValue = "这是默认返回值,接口调用失败";
            CloseableHttpClient httpClient = HttpClients.createDefault();
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            try{
                //第一步:创建HttpClient对象
             httpClient = HttpClients.createDefault();
                 
                 //第二步:创建httpPost对象
                HttpPost httpPost = new HttpPost(url);
                
                //第三步:给httpPost设置JSON格式的参数
                StringEntity requestEntity = new StringEntity(json,"utf-8");
                requestEntity.setContentEncoding("UTF-8");                
                httpPost.setHeader("Content-type", "application/json");
                httpPost.setEntity(requestEntity);
               
               //第四步:发送HttpPost请求,获取返回值
               returnValue = httpClient.execute(httpPost,responseHandler); //调接口获取返回值时,必须用此方法
    //           CloseableHttpResponse httpResonse = httpClient.execute(httpPost);
    //           int statusCode = httpResonse.getStatusLine().getStatusCode();
    //           if(statusCode!=200)
    //           {
    //                System.out.println("请求发送失败,失败的返回参数为:"+httpResonse.getStatusLine());
    //                returnValue = httpResonse.getStatusLine().toString();      
    //           }
    //           
              
            }
             catch(Exception e)
            {
                 e.printStackTrace();
            }
            
            finally {
               try {
                httpClient.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
             //第五步:处理返回值
             return returnValue;
        }
  • 相关阅读:
    区分DPI、分辨率(PPI)、图像的物理大小、像素宽度
    生成缩略图方法
    C#中调用Windows API的要点 .
    改变变量触发事件
    窗体异步示例
    Scroll滚动后发生的改变
    绘制不同颜色的文本图片
    字符串绘图片
    c#生成缩略图
    衔接UI线程和管理后台工作线程的类(多线程、异步调用)
  • 原文地址:https://www.cnblogs.com/fuckingPangzi/p/10019913.html
Copyright © 2011-2022 走看看