java跨越请求session不一致解决代码案例

package com.chuangda.jzgc.rygl.rldata;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class MainTest {
    public static final String ipAddr = "http://192.168.1.106:8080/scdeaps/";
    public static CookieStore cookieStore = new BasicCookieStore();
    public static void main(String[] args) throws ClientProtocolException, IOException {
        JSONObject json = JSONObject.fromObject(getToken());
        try {
            String access_token=json.getString("access_token");
            String timestamp=json.getString("timestamp");
            updata(timestamp,access_token);//通过口令验证,上传数据
            Thread.sleep(5000);
            updata(timestamp,access_token);//通过口令验证,上传数据
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }    
    public static String getToken()throws ClientProtocolException, IOException{
        CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpPost post = new HttpPost(MainTest.ipAddr+"tokenAction.do?method=token&appid=123456");
        // 设置 HttpClient 接收 Cookie,用与浏览器一样的策略  
        JSONObject obj = new JSONObject();
        CloseableHttpResponse response = client.execute(post);
        String mseeage=EntityUtils.toString(response.getEntity(),"utf-8");
        return mseeage;
    }
    public static void updata(String timestamp,String access_token) throws ClientProtocolException, IOException{
        HttpClient httpClient1 = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpPost post1 = new HttpPost(MainTest.ipAddr+"runDataAction.do?method=run&access_token="+access_token+"×tamp="+timestamp);
        JSONObject obj = new JSONObject();
        try {
            obj.put("lift", "电梯数据");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        List formParams = new ArrayList();   
        formParams.add(new BasicNameValuePair("data", obj.toString()));           
        HttpEntity entity1 = new UrlEncodedFormEntity(formParams, "UTF-8"); 
        post1.setEntity(entity1);
        HttpResponse response1 =  httpClient1.execute(post1);
    }
}
 

你可能感兴趣的:(java)