后台发送GET/POST方法
前言:
1,get请求
2,post请求
3,post,get通用方法
4,其他的get,post写法
正文:
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod;// HTTP GET request public static String SendGet(String url) {HttpClient client = new HttpClient();GetMethod method = new GetMethod(url);try {int statusCode = client.executeMethod(method);if (statusCode != HttpStatus.SC_OK) {logger.error("获取SendGet失败:" + method.getStatusLine());}return method.getResponseBodyAsString();} catch (HttpException e) {logger.error("获取SendGet失败: Fatal protocol violation", e);} catch (IOException e) {logger.error("获取SendGet失败: transport error", e);} finally {method.releaseConnection();}return ""; }
// HTTP POST public static String SendPOST(String url) {HttpClient client = new HttpClient();PostMethod method = new PostMethod(url);try {int statusCode = client.executeMethod(method);if (statusCode != HttpStatus.SC_OK) {logger.error("获取SendPOST失败:" + method.getStatusLine());}return method.getResponseBodyAsString();} catch (HttpException e) {logger.error("获取SendPOST失败: Fatal protocol violation", e);} catch (IOException e) {logger.error("获取SendPOST失败: transport error", e);} finally {method.releaseConnection();}return ""; }
(