HttpClient发起POST请求

来源:互联网 发布:游戏汉化器安卓软件 编辑:程序博客网 时间:2024/04/29 18:31

此函数返回请求结果

public static String executePost(String url, List<NameValuePair> params) {HttpPost httpPost = new HttpPost(url);//post请求,设置URL地址try {httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//设置请求参数和编码HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost);//得到请求的结果responseif (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {//如果状态码是200String result = EntityUtils.toString(httpResponse.getEntity(),HTTP.UTF_8);//得到结果, EntityUtils.toString()函数默认返回编码为ISO-8859-1,需要指定toString的第二个参数为UTF-8,否则中午乱码return result;}} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return "";}


0 0
原创粉丝点击