1. 程式人生 > >java跨越請求session不一致解決程式碼案例

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+"&timestamp="+timestamp);         JSONObject obj = new JSONObject();         try {             obj.put("lift", "電梯資料");         } catch (Exception e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         List<NameValuePair> formParams = new ArrayList<NameValuePair>();            formParams.add(new BasicNameValuePair("data", obj.toString()));                    HttpEntity entity1 = new UrlEncodedFormEntity(formParams, "UTF-8");          post1.setEntity(entity1);         HttpResponse response1 =  httpClient1.execute(post1);     } }