首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

用HttpClient处置Http请求

2012-07-01 
用HttpClient处理Http请求处理GET请求:StringBuffer sb new StringBuffer()HttpClient httpClient ne

用HttpClient处理Http请求

处理GET请求:

StringBuffer sb = new StringBuffer();    HttpClient httpClient = new DefaultHttpClient();HttpPost httpPost = new HttpPost(url);//post方式时,需要用NameValuePair数组传递参数List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  nameValuePairs.add(new BasicNameValuePair("username", "cjm陈"));  nameValuePairs.add(new BasicNameValuePair("password", "123"));  httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));  HttpResponse response = httpClient.execute(httpPost); if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){HttpEntity entity = response.getEntity();if(entity!=null){BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));String line = null;while((line=reader.readLine())!=null){sb.append(line + "\n");}reader.close();}}

?

热点排行