commons-logging.jar 还有 commons-codec.jar 这两个包没有就报错咯
用的httpclient 4.0 beta2
Code
package com.testhttpclient;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
//import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class MainTest {
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();
}
}
}
package com.testhttpclient;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
//import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
public class MainTest {
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();
}
}
}