Java httpUrlConn调用之间未维护会话
我有以下方法:Java httpUrlConn调用之间未维护会话,java,android,session,httpurlconnection,Java,Android,Session,Httpurlconnection,我有以下方法: public static String postJSONObject(String myurl, JSONObject parameters, Context ctx) { HttpURLConnection conn = null; try { StringBuffer response = null; URL url = new URL(myurl); conn =
public static String postJSONObject(String myurl, JSONObject parameters, Context ctx) {
HttpURLConnection conn = null;
try {
StringBuffer response = null;
URL url = new URL(myurl);
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(parameters.toString());
Log.d("Parameters to string", "params.tostring" + parameters.toString());
writer.close();
out.close();
int responseCode = conn.getResponseCode();
System.out.println("responseCode" + responseCode);
switch (responseCode) {
case 200:
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
List cookieList = conn.getHeaderFields().get("Set-Cookie");
if (cookieList != null) {
for (Object cookieTemp : cookieList) {
CookieManager.getInstance().setCookie(conn.getURL().toString(), cookieTemp.toString());
}
}
String cookie = CookieManager.getInstance().getCookie(new URL("https://exaperth.exacrm.com").getHost());
System.out.println("Cookie after adding " + cookie);
return response.toString();
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (conn != null) {
try {
conn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
return null;
}
我将用户名和密码发布到服务器,服务器创建会话,“设置Cookie”保存到CookieManager中,如下所示:
PHPSESSID=a3d2367f8a5a3221e9bad1a91a34fd55; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%221381c152699fb61d04663c9b854ecdd7%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%22182.59.245.196%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A60%3A%22Dalvik%2F2.1.0+%28Linux%3B+U%3B+Android+5.1.1%3B+Nexus+5+Build%2FLMY48B%29%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1447140920%3B%7Ddd2c014724b9ca061b50774f1fea235d
在下面的第二种方法中:
public static String doHttpUrlConnectionAction(String desiredUrl, String cookieValue) throws Exception {
URL url = null;
BufferedReader reader = null;
StringBuilder stringBuilder;
try {
url = new URL(desiredUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// just want to do an HTTP GET here
connection.setRequestMethod("POST");
// connection.setDoOutput(true);
// give it 15 seconds to respond
String cookie = CookieManager.getInstance().getCookie(new URL("https://exaperth.exacrm.com").getHost());
System.out.println("Cookie from cookie store: " + cookie);
connection.setRequestProperty("Cookie", cookie);
connection.setReadTimeout(15 * 1000);
connection.connect();
// read the output from the server
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
// close the reader; this can throw an exception too, so
// wrap it in another try/catch block.
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
我使用相同的cookie,但不知何故,会话无效,我得到了登录页面。我错过了什么