Java (Android)HttpClient、URL和HttpURLConnection中的Cookie问题
我将在android应用程序的注册表中添加验证码验证。但我发现服务器向我发出信号,表示没有会话。最后,我发现我的应用程序的cookie没有存储任何内容 即使验证正确,我也无法注册帐户。我确信我的php服务器没有问题,因为我已经在浏览器中测试过了 我已经建立了三种类型的连接来测试cookie问题 1) HttpClientJava (Android)HttpClient、URL和HttpURLConnection中的Cookie问题,java,php,android,cookies,captcha,Java,Php,Android,Cookies,Captcha,我将在android应用程序的注册表中添加验证码验证。但我发现服务器向我发出信号,表示没有会话。最后,我发现我的应用程序的cookie没有存储任何内容 即使验证正确,我也无法注册帐户。我确信我的php服务器没有问题,因为我已经在浏览器中测试过了 我已经建立了三种类型的连接来测试cookie问题 1) HttpClient HttpResponse response = httpclient.execute(httppost); 2) 网址 3) HttpURLConnection priva
HttpResponse response = httpclient.execute(httppost);
2) 网址
3) HttpURLConnection
private void send_post(HttpURLConnection connection,String variable_set) throws Exception
{
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Content-Length", "" + Integer.toString(variable_set.getBytes().length));
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(variable_set);
wr.flush();
wr.close();
}
另外,我将此代码添加到设置cookie中
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
不幸的是,只有使用cookie和服务器的HttpURLConnection成功显示存在一些会话变量。然而,我的应用程序主要使用URL和HttpClient
有人能告诉我如何在HttpClient和URL中启用cookie吗?下面是一个示例代码,用于在Android中将JSESSIONID(Apache Tomcat服务器的)设置为cookie。希望这将为您提供一些在PHP中执行类似过程的想法:
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setCookieStore(new BasicCookieStore());
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID<<or your variable name>>", <<ACTUAL VALUE>>);
cookie.setDomain(<<SET YOUR DOMAIN NAME HERE>>);
cookie.setPath("/");
httpClient.getCookieStore().addCookie(cookie);
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json"); //use text/plain if response if HTTP response is plain text
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
if (formData != null) {
httpPost.setEntity(new UrlEncodedFormEntity(formData, HTTP.UTF_8));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
if (formData != null) {
String paramString = URLEncodedUtils.format(formData, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
status_code = httpResponse.getStatusLine().getStatusCode();
httpEntity = httpResponse.getEntity();
Log.i("Status Code After Login", "> >status code > >" + status_code + "> >url > >"+url);
if(status_code==200)
response = EntityUtils.toString(httpEntity);
else
response="fail";
<<response will contain the final HTTP response>>
DefaultHttpClient-httpClient=newdefaulthttpclient();
setCookieStore(新的BasicCookieStore());
BasicClientCookie=新的BasicClientCookie(“JSESSIONID”);
cookie.setDomain();
cookie.setPath(“/”);
httpClient.getCookieStore().addCookie(cookie);
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
//检查http请求方法类型
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
setHeader(“Accept”,“application/json”);//如果HTTP响应为纯文本,则使用text/plain if response
setHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
if(formData!=null){
setEntity(新的UrlEncodedFormEntity(formData,HTTP.UTF_8));
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
if(formData!=null){
String paramString=URLEncodedUtils.format(formData,“utf-8”);
url+=“?”+参数字符串;
}
HttpGet HttpGet=新的HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
status_code=httpResponse.getStatusLine().getStatusCode();
httpEntity=httpResponse.getEntity();
Log.i(“登录后的状态代码”,“>>状态代码>>”+状态代码+“>>url>>”+url);
如果(状态代码==200)
response=EntityUtils.toString(httpEntity);
其他的
response=“fail”;
DefaultHttpClient httpClient = new DefaultHttpClient();
httpClient.setCookieStore(new BasicCookieStore());
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID<<or your variable name>>", <<ACTUAL VALUE>>);
cookie.setDomain(<<SET YOUR DOMAIN NAME HERE>>);
cookie.setPath("/");
httpClient.getCookieStore().addCookie(cookie);
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Accept", "application/json"); //use text/plain if response if HTTP response is plain text
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
if (formData != null) {
httpPost.setEntity(new UrlEncodedFormEntity(formData, HTTP.UTF_8));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
if (formData != null) {
String paramString = URLEncodedUtils.format(formData, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
status_code = httpResponse.getStatusLine().getStatusCode();
httpEntity = httpResponse.getEntity();
Log.i("Status Code After Login", "> >status code > >" + status_code + "> >url > >"+url);
if(status_code==200)
response = EntityUtils.toString(httpEntity);
else
response="fail";
<<response will contain the final HTTP response>>