文档章节

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
浦东

暂无文章

【大福利】极客时间专栏返现二维码大汇总

我已经购买了如下专栏,大家通过我的二维码你可以获得一定额度的返现! 然后,再给大家来个福利,只要你通过我的二维码购买,并且关注了【飞鱼说编程】公众号,可以加我微信或者私聊我,我再...

飞鱼说编程
今天
1
0
Spring5对比Spring3.2源码之容器的基本实现

最近看了《Spring源码深度解析》,该书是基于Spring3.2版本的,其中关于第二章容器的基本实现部分,目前spring5的实现方式已有较大改变。 Spring3.2的实现: public void testSimpleLoad(){...

Ilike_Java
今天
1
0
【王阳明心学语录】-001

1.“破山中贼易,破心中贼难。” 2.“夫万事万物之理不外于吾心。” 3.“心即理也。”“心外无理,心外无物,心外无事。” 4.“人心之得其正者即道心;道心之失其正者即人心。” 5.“无...

卯金刀GG
今天
2
0
OSChina 周三乱弹 —— 我们无法成为野兽

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @ _刚刚好: 霸王洗发水这波很骚 手机党少年们想听歌,请使劲儿戳(这里) hahahahahahh @嘻酱:居然忘了喝水。 让你喝可乐的话, 你准忘不了...

小小编辑
今天
10
0
vm GC 日志 配置及查看

-XX:+PrintGCDetails 打印 gc 日志 -XX:+PrintTenuringDistribution 监控晋升分布 -XX:+PrintGCTimeStamps 包含时间戳 -XX:+printGCDateStamps 包含时间 -Xloggc:<filename> 可以将数据保存为......

Canaan_
昨天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部