文档章节

http请求

菜鸟上路中
 菜鸟上路中
发布于 2016/04/18 19:16
字数 506
阅读 5
收藏 0
点赞 1
评论 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
浦东

暂无相关文章

Jenkins实践3 之脚本

#!/bin/sh# export PROJ_PATH=项目路径# export TOMCAT_PATH=tomcat路径killTomcat(){pid=`ps -ef | grep tomcat | grep java|awk '{print $2}'`echo "tom...

晨猫 ⋅ 今天 ⋅ 0

Spring Bean的生命周期

前言 Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解。 首先看下生命周期图: 再谈生命周期之前有一点需要先明确: Spring 只帮我们管理单例模...

素雷 ⋅ 今天 ⋅ 0

zblog2.3版本的asp系统是否可以超越卢松松博客的流量[图]

最近访问zblog官网,发现zlbog-asp2.3版本已经进入测试阶段了,虽然正式版还没有发布,想必也不久了。那么作为aps纵横江湖十多年的今天,blog2.2版本应该已经成熟了,为什么还要发布这个2.3...

原创小博客 ⋅ 今天 ⋅ 0

聊聊spring cloud的HystrixCircuitBreakerConfiguration

序 本文主要研究一下spring cloud的HystrixCircuitBreakerConfiguration HystrixCircuitBreakerConfiguration spring-cloud-netflix-core-2.0.0.RELEASE-sources.jar!/org/springframework/......

go4it ⋅ 今天 ⋅ 0

二分查找

二分查找,也称折半查找、二分搜索,是一种在有序数组中查找某一特定元素的搜索算法。搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束;如果某一特定元素大于...

人觉非常君 ⋅ 今天 ⋅ 0

VS中使用X64汇编

需要注意的是,在X86项目中,可以使用__asm{}来嵌入汇编代码,但是在X64项目中,再也不能使用__asm{}来编写嵌入式汇编程序了,必须使用专门的.asm汇编文件来编写相应的汇编代码,然后在其它地...

simpower ⋅ 今天 ⋅ 0

ThreadPoolExecutor

ThreadPoolExecutor public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, ......

4rnold ⋅ 昨天 ⋅ 0

Java正无穷大、负无穷大以及NaN

问题来源:用Java代码写了一个计算公式,包含除法和对数和取反,在页面上出现了-infinity,不知道这是什么问题,网上找答案才明白意思是负的无穷大。 思考:为什么会出现这种情况呢?这是哪里...

young_chen ⋅ 昨天 ⋅ 0

前台对中文编码,后台解码

前台:encodeURI(sbzt) 后台:String param = URLDecoder.decode(sbzt,"UTF-8");

west_coast ⋅ 昨天 ⋅ 0

实验楼—MySQL基础课程-挑战3实验报告

按照文档要求创建数据库 sudo sercice mysql startwget http://labfile.oss.aliyuncs.com/courses/9/createdb2.sqlvim /home/shiyanlou/createdb2.sql#查看下数据库代码 代码创建了grade......

zhangjin7 ⋅ 昨天 ⋅ 0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部