文档章节

http请求

菜鸟上路中
 菜鸟上路中
发布于 2016/04/18 19:16
字数 506
阅读 5
收藏 0

public  boolean getSeasonIngredientList() throws ParseException, IOException {

            HttpClient client = new DefaultHttpClient();

            Map<String, String> paramMap = new HashMap<String, String>();

            DouguoRequestParameter parameter= new DouguoRequestParameter();

            String url = ClientConstant. DOUGUONEW_API_URL;

             parameter.setMethod( DOUGUONEW_RECIPE_SEASONSHICAI);//参数一

             parameter.setApp_key( DOUGUONEW_APP_KEY);//参数二

             this.makeSign( parameter);

            String sign = parameter.getSign(); //参数三

            

          String a = "method=" + "douguo.recipe.seasonshicai" + "&app_key=" "98926612" + "&sign=" + sign ;

        HttpGet post = new HttpGet( url+ "?"+ a);  //HttpGet

       

        /**

         * 以下为HttpPost方式请求 -----①将参数封装成一个对象存入post中,进行请求

         * ②用list来存放参数及其参数所对应的值,最后将post.setEntity()将存好参数的list存入post中,进行请求。

         */

//        List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); 

//        nvps.add(new BasicNameValuePair("method", "douguo.recipe.seasonshicai")); 

//        nvps.add(new BasicNameValuePair("app_key", "98926612"));

//        nvps.add(new BasicNameValuePair("sign", sign));

//        parameter.setSign(sign);

       

       

        //StringEntity se = new StringEntity(a, "UTF-8");

//        post.setEntity(se);

//        post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));

            HttpResponse response = client.execute( post);

          BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();

        String line = "";

        while ((line = rd.readLine()) != null) {

            result.append( line);

        }

        System. out.println( result.toString());

          return false;

      }


注释部分为用map方式存放参数以其对应参数值,来达到封装post的请求。-------------------最后选择HttpGet/HttpPost方式


protected static final String uploadPath = "/upload";


      HttpClient client = new DefaultHttpClient();


      protected static final String postPath = "/api/send";

      

      String appMasterSecret = "wlkzu5wcstiwipag7nqzs1wn8ik9ooau";

      

      public boolean send(UmengRequestPushInfo pushInfo) throws Exception {

        String url = ClientConstant. UMENG_API_URL + postPath ;

        String payload = GsonUtil. toJson(pushInfo);       

        String str = "POST" + url + payload + appMasterSecret;

        String sign = DigestUtils. md5Hex((str).getBytes("utf8"));

        url = url + "?sign=" + sign;

        HttpPost post = new HttpPost( url);

        StringEntity se = new StringEntity( payload, "UTF-8");

        post.setEntity(se);

        // Send the post request and get the response

        HttpResponse response = client.execute( post);

        int status = response.getStatusLine().getStatusCode();

        System. out.println( "Response Code : " + status );

        BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();

        String line = "";

        while ((line = rd.readLine()) != null) {

            result.append( line);

        }

        System. out.println( result.toString());

        if (status == 200) {

            System. out.println( "Notification sent successfully.");

        } else {

            System. out.println( "Failed to send the notification!");

        }

        return true;

    }


      

      public String uploadContents(String appkey,String appMasterSecret,String contents ) throws Exception {

             // Construct the json string

            JSONObject uploadJson = new JSONObject();

             uploadJson.put( "appkey", appkey);

            String timestamp = Integer.toString((int)(System.currentTimeMillis() / 1000));

             uploadJson.put( "timestamp", timestamp);

             uploadJson.put( "content", contents);

             // Construct the request

            String url = ClientConstant. UMENG_API_URL + uploadPath ;

            String postBody = uploadJson.toString();

            String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex(( "POST" + url + postBody + appMasterSecret).getBytes( "utf8"));

             url = url + "?sign=" + sign;

            HttpPost post = new HttpPost( url);

            StringEntity se = new StringEntity( postBody, "UTF-8");

             post.setEntity( se);

             // Send the post request and get the response

            HttpResponse response = client.execute( post);

            System. out.println( "Response Code : " + response.getStatusLine().getStatusCode());

            BufferedReader rd = new BufferedReader( new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();

            String line = "";

             while (( line = rd.readLine()) != null) {

                   result.append( line);

            }

            System. out.println( result.toString());

             // Decode response string and get file_id from it

            JSONObject respJson = new JSONObject();

            String ret = respJson.getString( "ret");

             if (! ret.equals( "SUCCESS")) {

                   throw new Exception( "Failed to upload file");

            }

            JSONObject data = respJson.getJSONObject( "data");

            String fileId = data.getString( "file_id");

            

             return fileId;

      }


© 著作权归作者所有

共有 人打赏支持
菜鸟上路中
粉丝 1
博文 14
码字总数 7161
作品 0
浦东

暂无文章

人生苦短:Python里的17个“超赞操作

人生苦短,我选Python”。那么,你真的掌握了Python吗? 1. 交换变量 有时候,当我们要交换两个变量的值时,一种常规的方法是创建一个临时变量,然后用它来进行交换。比如: # 输入 a = 5 b ...

糖宝lsh
38分钟前
4
0
咕泡-spring中常用设计模式概述

设计模式就是经验之谈,供后人借鉴,解决一些具有代表性的问题 设计模式来源于生活,反过来帮助我们更好生活 设计模式提升代码的可读性、可扩展性、维护成本、复杂业务问题 千万不要死记硬背...

职业搬砖20年
今天
2
0
day59-20180817-流利阅读笔记-待学习

假·照骗,真·社交焦虑 雪梨 2018-08-17 1.今日导读 发朋友圈之前,不少人为了展现更美好的生活状态会对照片加以“微调”,或是加个滤镜显得逼格更高,或是磨个皮瘦个脸拉个大长腿。现在,国...

aibinxiao
今天
18
0
OSChina 周五乱弹 —— 姑娘在这个节日里表白你接受么?

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @Sharon啊:完全被这个小姐姐圈粉了,学两首她的歌去哈哈 分享王贰浪的单曲《往后余生(翻自 马良)》 《往后余生(翻自 马良)》- 王贰浪 手...

小小编辑
今天
846
16
为什么HashMap要自己实现writeObject和readObject方法?

为什么HashMap要自己实现writeObject和readObject方法? 如果你有仔细阅读过HashMap的源码,那么你一定注意过一个问题:HashMap中有两个私有方法。 private void writeObject(java.io.Objec...

DemonsI
今天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部