java后端模拟http请求_java模拟http请求

这个Java类展示了如何使用Apache HttpClient库进行带cookie的GET和POST请求。它设置了连接超时和socket超时,并处理HTTP响应。登录方法通过POST提交用户名和密码,然后获取响应。

org.apache.httpcomponents

httpclient

4.2

public class HttpUtil {

private static DefaultHttpClient defaultHttpClient = null;

public static CookieStore cookieStore;

public static final int CONNECTIONTIMEOUT = 300000;

public static final int SOCKETTIMEOUT = 300000;

/**

* 带cookie的get提交

*

* @param urlpath

* @param encoding

* @return

*/

public static String doHttpGetSetCookie(String urlpath, String encoding) {

HttpGet request = new HttpGet(urlpath);

try {

HttpParams httpParams = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTIONTIMEOUT);

HttpConnectionParams.setSoTimeout(httpParams, SOCKETTIMEOUT);

if (defaultHttpClient == null) {

defaultHttpClient = new DefaultHttpClient(httpParams);

}

if (cookieStore != null) {

defaultHttpClient.setCookieStore(cookieStore);

}

HttpResponse response = defaultHttpClient.execute(request);

cookieStore = defaultHttpClient.getCookieStore();

if (response.getStatusLine().getStatusCode() == 200) {

Header str = response.getLastHeader("content-type");

if(str!=null&&str.toString().contains("text/html")){

System.out.println(response.getLastHeader("content-type"));

}

String string = EntityUtils.toString(response.getEntity(), encoding);

return string;

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

/**

* 带cookie的post提交

*

* @param uri

* @param params

* @return

*/

public static String doHttpPostSetCookie(String uri, List params) {

HttpPost hp = new HttpPost(uri);

try {

if (params != null) {

hp.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

}

if (defaultHttpClient == null) {

defaultHttpClient = new DefaultHttpClient();

}

defaultHttpClient.getParams().setParameter("http.connection.timeout", CONNECTIONTIMEOUT);

defaultHttpClient.getParams().setParameter("http.socket.timeout", SOCKETTIMEOUT);

if (cookieStore != null) {

defaultHttpClient.setCookieStore(cookieStore);

}

HttpResponse response = defaultHttpClient.execute(hp);

cookieStore = defaultHttpClient.getCookieStore();

if (response.getStatusLine().getStatusCode() == 200) {

HttpEntity entity = response.getEntity();

String string = EntityUtils.toString(entity,"UTF-8");

return string;

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static void main(String[] args) {

login();

}

private static void login() {

List params = new ArrayList();

params.add(new BasicNameValuePair("j_username", "admin"));

params.add(new BasicNameValuePair("j_password", "123456"));

String str = HttpUtil.doHttpPostSetCookie("http://127.0.0.1/j_spring_security_check",params);

System.out.println(str);

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值