文档章节

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
浦东
私信 提问

暂无文章

jquery通过id显示隐藏

var $div3 = $('#div3'); 显示 $div3.show(); 隐藏 $div3.hide();

yan_liu
今天
3
0
《乱世佳人》读书笔记及相关感悟3900字

《乱世佳人》读书笔记及相关感悟3900字: 之前一直听「荔枝」,后来不知怎的转向了「喜马拉雅」,一听就是三年。上班的时候听房产,买房了以后听装修,兴之所至时听旅行,分手后听亲密关系,...

原创小博客
今天
3
0
大数据教程(9.6)map端join实现

上一篇文章讲了mapreduce配合实现join,本节博主将讲述在map端的join实现; 一、需求 实现两个“表”的join操作,其中一个表数据量小,一个表很大,这种场景在实际中非常常见,比如“订单日志...

em_aaron
今天
3
0
cookie与session详解

session与cookie是什么? session与cookie属于一种会话控制技术.常用在身份识别,登录验证,数据传输等.举个例子,就像我们去超市买东西结账的时候,我们要拿出我们的会员卡才会获取优惠.这时...

士兵7
今天
3
0
十万个为什么之为什么大家都说dubbo

Dubbo是什么? 使用背景 dubbo为什么这么流行, 为什么大家都这么喜欢用dubbo; 通过了解分布式开发了解到, 为适应访问量暴增,业务拆分后, 子应用部署在多台服务器上,而多台服务器通过可以通过d...

尾生
今天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部