用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();}}
?