jar包:
public class HttpUtil{
public static String httpRequestToString(String url, String requestMethod){
String methodResult = null;
try {
boolean isGet = "get".equalsIgnoreCase(requestMethod);
boolean isPost = "post".equalsIgnoreCase(requestMethod);
boolean isPut = "put".equalsIgnoreCase(requestMethod);
boolean isDelete = "delete".equalsIgnoreCase(requestMethod);
DefaultHttpClient client = new DefaultHttpClient();
HttpRequestBase method = null;
if(isGet){
method = new HttpGet(url);
}else if(isPost){
method = new HttpPost(url);
HttpPost postMethod = (HttpPost) method;
}else if(isPut){
method = new HttpPut(url);
HttpPut putMethod = (HttpPut) method;
}else if(isDelete){
method = new HttpDelete(url);
}
method.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000);
method.addHeader("Content-Type","application/x-www-form-urlencoded");
HttpClientContext context = null;
HttpResponse response = client.execute(method, context);
if(response.getStatusLine().getStatusCode()==200){
methodResult = EntityUtils.toString(response.getEntity());
}
client.close();
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
return methodResult;
}
}