commons-logging.jar 还有 commons-codec.jar 这两个包没有就报错咯
用的httpclient 4.0 beta2
public static void main(String[] args) {
String url ="
HttpClient httpClient=new DefaultHttpClient();
//httpClient.getParams().setParameter(HttpClientParams.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.BROWSER_COMPATIBILITY);
//get 方式
HttpGet httpget = new HttpGet(url);
httpget.setHeader("Accept-Language","zh-cn");//
try{
HttpResponse response = httpClient.execute(httpget);
HttpEntity entity = response.getEntity();
String webcode = new String(EntityUtils.toString(response.getEntity()).getBytes("ISO-8859-1"),"UTF-8");//具体情况具体分析
if(entity!=null){
try{
entity.consumeContent();
}catch(IOException e){
e.printStackTrace();
}
}
}catch(Exception ex){
ex.printStackTrace();
}
//post方式
HttpPost httppost = new HttpPost(url);
httppost.setHeader("
// NameValuePair[] nvps = new NameValuePair[] {
// new BasicNameValuePair("
// new BasicNameValuePair("
// };
// httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("
try{
httppost.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
String webcode = EntityUtils.toString(response.getEntity());
if(entity!=null){
try{
entity.consumeContent();
}catch(IOException e){
e.printStackTrace();
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
}