zoukankan      html  css  js  c++  java
  • httpclient发送不带参数post数据

    两个问题:
         1、httpclient怎样发送一个没有不论什么參数的post数据呢?
         2、Webproject怎样去接收一个无參数的post呢?

    起因:
         今天(2014.11.10)在开发中碰到了一个问题。接口提供方提供的接口是要求使用post方式发送数据的。心想这不超简单的一个东西吗?直接post过去不就是了。可是,提供的接口是没有不论什么參数的。不是类似这样的http://api.dutycode.com/data/parm=xxx这样的接口,而是http://api.dutycode.com/data。这个地址直接接收post数据。
         话说,当时瞬间心碎了,没接触过啊。。。

         可是,总归是有解决的方法的。既然有这种接口来接收数据,那么一定能够发送
    so

    解决的方法:非常easy
         实现代码例如以下:
         
    public static void main(String[] args) throws Exception {
                HttpClient client = HttpClients. createDefault();
                
                HttpPost post = new HttpPost("http://127.0.0.1/report/testPost" );
                
                 //组装一个 json串。用于发送
                JSONObject jsonObj = new JSONObject();
                jsonObj.put( "website" "http://www.dutycode.com" );
                jsonObj.put( "email" "dutycode@gmail.com" );
                
                StringEntity entity = new StringEntity(jsonObj.toJSONString());
                entity.setContentEncoding( "UTF-8" );
                entity.setContentType( "application/json" );//设置为 json数据
                
                post.setEntity(entity);
                
                HttpResponse response = client.execute(post);
                
                HttpEntity resEntity = response.getEntity();
                String res = EntityUtils. toString(resEntity);
                
                System. out .println(res);
          }

    问题2 Webproject怎样去接收一个无參数的post呢?

         既然能发送,那么得想办法实现服务端啊,要不然怎么才干死心。

         so
         測试代码:(注,使用公司内部框架实现。但基本原理是一样的)
         
    @Path ("testPost" )
     public ActionResult getpost() throws Exception{
                StringBuilder sb = new StringBuilder ();
                InputStream is = getRequest().getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                 byte [] buffer = new byte[1024];
                 int read = 0;
                 while ((read=bis.read(buffer)) != -1){
                      sb.append( new String(buffer, 0, read, "UTF-8" ));
                }
                
                System. out .println(sb.toString());
                 return outputStream("{msg:success}" );
    }

        原理非常easy。直接获取到post过来的全部数据流


    上面两个结合起来一起測试的话,结果例如以下:
         第一段代码返回结果:
              
    {msg:success}
         第二段代码返回结果:
    {"email":"dutycode@gmail.com","website":"http://www.dutycode.com"}


    版权全部:《攀爬蜗牛》 => 《httpclient发送无參数的post数据
    本文地址:http://www.dutycode.com/post-76.html
    除非注明。文章均为 《蜗牛爬》 原版的,欢迎转载!地址转载本文,请注明,谢谢

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    css3-10 如何使用滚动条
    jquery如何实现点击标题收缩下面的内容
    js 字符串操作函数有哪些
    好记性不如烂笔头85-spring3学习(6)-BeanFactory 于bean生命周期
    【电视桌面CSWUI】电视桌面(launcher)截图欣赏
    OR1200数据Cache运用情景分析
    Spring3.0学习笔记文档的官方网站(六)--3.4.1
    or1200乘法除法指令解释
    C++使用简单的函数指针
    jQuery表格排序总成-tablesorter
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4677091.html
Copyright © 2011-2022 走看看