关闭

HttpClient的Post请求

110人阅读 评论(0) 收藏 举报
分类:
public static String postHttp(String url,String key,String menu){

        try {
            HttpClient hc=new DefaultHttpClient();
            HttpPost gp=new HttpPost(url);

            List<NameValuePair> parameters=new ArrayList<NameValuePair>();
            BasicNameValuePair b=new BasicNameValuePair("key", key);
            BasicNameValuePair b2=new BasicNameValuePair("menu", menu);

            parameters.add(b);
            parameters.add(b2);
            UrlEncodedFormEntity entity=new UrlEncodedFormEntity(parameters, "UTF-8");
            gp.setEntity(entity);

            HttpResponse execute = hc.execute(gp);
            int code = execute.getStatusLine().getStatusCode();
            if(code == HttpStatus.SC_OK){
                HttpEntity httpEntity = execute.getEntity();
                httpToString = EntityUtils.toString(httpEntity, "UTF-8");
            }

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return httpToString;
    }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3102次
    • 积分:206
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类