需要在同一会话中,处理2次请求的做法

1 先获取第一请求的cookie信息

2 把第一次获取的cookie携带给第2次请求


代码如下:






import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.HttpStatus;

import org.apache.commons.io.IOUtils;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.params.CookiePolicy;

import org.apache.http.client.params.HttpClientParams;

import org.apache.http.cookie.Cookie;

import org.apache.http.impl.client.DefaultHttpClient;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;



import java.io.IOException;

import java.util.List;



public class SmsHttpUtil {





    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);



    /**

     * 发送POST请求

     *

     * @param reqUrl URL地址

     * @return 字符串

     * @throws java.io.IOException

     */

    public static String sendPostOfGetPwd(String reqUrl) throws IOException {



        DefaultHttpClient httpClient = null;



        HttpEntity entity = null;

        try {

            httpClient = new DefaultHttpClient();



            HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);

            HttpPost httpPost = new HttpPost(reqUrl);



            HttpResponse response = httpClient.execute(httpPost);



            List cookies = httpClient.getCookieStore().getCookies();



            String rtnCookies = "";

            for (Cookie cookie : cookies) {

                if ("jsessionid".equalsIgnoreCase(cookie.getName()) ||

                        "MMwwwURLtel".equalsIgnoreCase(cookie.getName())) {

                    rtnCookies += ";" + cookie.getName() + "=" + cookie.getValue();

                }

            }



            rtnCookies = rtnCookies.substring(1);

            int statusCode = response.getStatusLine().getStatusCode();

            logger.info("获取短信返回状态statusCode===" + statusCode);

            if (statusCode != HttpStatus.SC_OK) {

                //从头中取出转向的地址

                logger.info("返回状态是" + statusCode);

                return null;

            }

            entity = response.getEntity();

            String strResp = IOUtils.toString(entity.getContent(), "UTF-8");



            if (strResp == null || strResp.equals("")) {

                return null;

            }

            return strResp + "|" + rtnCookies;





        } catch (HttpException e) {

            //发生致命的异常,可能是协议不对或者返回的内容有问题

            logger.error("Please check your provided http address!");

            return null;

        } catch (IOException e) {

            //发生网络异常

            logger.info("网络异常" + reqUrl, e);

            return null;

        } finally {

            if (httpClient != null) {

                httpClient.getConnectionManager().shutdown();

            }

        }





    }





    /**

     * 发送POST请求

     *

     * @param reqUrl URL地址

     * @return 字符串

     * @throws java.io.IOException

     */

    public static String sendPostOfCheckPwd(String reqUrl, String cookieValue) throws IOException {



        DefaultHttpClient httpClient = null;



        HttpEntity entity = null;

        try {

            httpClient = new DefaultHttpClient();



            HttpClientParams.setCookiePolicy(httpClient.getParams(), CookiePolicy.BROWSER_COMPATIBILITY);



            HttpPost httpPost = new HttpPost(reqUrl);

            httpPost.setHeader("Cookie", cookieValue);

            HttpResponse response = httpClient.execute(httpPost);



            int statusCode2 = response.getStatusLine().getStatusCode();

            logger.info("短信验证返回statusCode===" + statusCode2);

            if (statusCode2 != HttpStatus.SC_OK) {

                //从头中取出转向的地址

                logger.info("返回状态是" + statusCode2);

                return null;

            }

            entity = response.getEntity();

            String strResp = IOUtils.toString(entity.getContent(), "UTF-8");



            if (strResp == null || strResp.equals("")) {

                return null;

            }

            return strResp;





        } catch (HttpException e) {

            //发生致命的异常,可能是协议不对或者返回的内容有问题

            logger.error("Please check your provided http address!");

            return null;

        } catch (IOException e) {

            //发生网络异常

            logger.info("网络异常" + reqUrl, e);

            return null;

        } finally {

            if (httpClient != null) {

                httpClient.getConnectionManager().shutdown();

            }

        }





    }





}

你可能感兴趣的:(apache,网络协议)