Android 存储用户凭据的更好方法
好的,我正在尝试为iPhone和Android开发一个移动网站应用程序。目前,我的站点使用cURL将用户登录到另一个站点。我有一个基于用户用户名创建cookie的PHP脚本。cURL然后将信息放入该cookie中。cookie存储在我的站点的主机上 基本上,我正在创建的这个移动网站应该允许用户登录我开发的论坛,因为网站所有者不允许我在他们的网站上创建移动版本,所以需要在我的网站上创建。一旦他们登录,他们就可以阅读帖子并回复。当它开始阅读时,线程需要加载cookie,以及当他们试图发表文章时 如何将cookie保存到用户手机而不是服务器?我问这个问题的原因是,我希望我的主机不会被几十个我不想看到的带有用户凭据的文本文件填满,这样我就不会钓鱼了 我想让用户登录,cookie保存到手机上。他们想看一篇帖子,电话调出了那块饼干。他们想发帖,电话把饼干拉了出来 我查看了PHP setcookie函数,不确定这是否是我所需要的Android 存储用户凭据的更好方法,android,iphone,web-applications,curl,session-cookies,Android,Iphone,Web Applications,Curl,Session Cookies,好的,我正在尝试为iPhone和Android开发一个移动网站应用程序。目前,我的站点使用cURL将用户登录到另一个站点。我有一个基于用户用户名创建cookie的PHP脚本。cURL然后将信息放入该cookie中。cookie存储在我的站点的主机上 基本上,我正在创建的这个移动网站应该允许用户登录我开发的论坛,因为网站所有者不允许我在他们的网站上创建移动版本,所以需要在我的网站上创建。一旦他们登录,他们就可以阅读帖子并回复。当它开始阅读时,线程需要加载cookie,以及当他们试图发表文章时 如何
感谢您提供的任何帮助。当您在服务器端设置cookie时,cookie会通过一种称为HTTP头的东西发送到您的手机客户端。有一个名为Set Cookie和Cookie值的HTTP头。当浏览器将来向服务器发出请求时,它将在一个名为Cookie的HTTP头中返回该值 因此,如果您想设置一个cookie并使用该cookie,则需要从您的请求中获取cookie,将其存储在安全的地方,并在将来的请求中返回
public static InputStream getDataFromHTTP(String url, String authenticationCookie, String mimetype) throws ClientProtocolException, IOException
{
DefaultHttpClient client = getHttpClient();
if (client == null)
throw new IOException("Cant getHttpClient()");
if (url == null)
throw new IOException("URL is null");
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Accept", mimetype);
httpget.addHeader("Cookie", authenticationCookie);
httpget.addHeader("Accept-Encoding", "gzip");
HttpResponse response = client.execute(httpget);
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
return instream;
}
下面是一个简单的身份验证方法,它接受url、用户名和密码并返回cookie值
static public String authenticate(String service_url, String username, String password) throws IOException
{
if (username == null || password == null)
throw new IOException();
String charset = "UTF-8";
URL url = new URL(service_url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="+charset);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setReadTimeout(5000); // 2 second timeout.
String query = String.format("Email=%s&Password=%s",
URLEncoder.encode(username, charset),
URLEncoder.encode(password, charset));
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
connection.getInputStream();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
if (cookies == null)
throw new IOException();
for (String cookie : cookies)
{
if (cookie.startsWith("authcookie"))
return cookie; // this is the only correct path out.
}
throw new IOException();
}
当您在服务器端设置cookie时,cookie会通过一种称为HTTP头的东西发送到您的手机客户端。有一个名为Set Cookie和Cookie值的HTTP头。当浏览器将来向服务器发出请求时,它将在一个名为Cookie的HTTP头中返回该值 因此,如果您想设置一个cookie并使用该cookie,则需要从您的请求中获取cookie,将其存储在安全的地方,并在将来的请求中返回
public static InputStream getDataFromHTTP(String url, String authenticationCookie, String mimetype) throws ClientProtocolException, IOException
{
DefaultHttpClient client = getHttpClient();
if (client == null)
throw new IOException("Cant getHttpClient()");
if (url == null)
throw new IOException("URL is null");
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Accept", mimetype);
httpget.addHeader("Cookie", authenticationCookie);
httpget.addHeader("Accept-Encoding", "gzip");
HttpResponse response = client.execute(httpget);
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
return instream;
}
下面是一个简单的身份验证方法,它接受url、用户名和密码并返回cookie值
static public String authenticate(String service_url, String username, String password) throws IOException
{
if (username == null || password == null)
throw new IOException();
String charset = "UTF-8";
URL url = new URL(service_url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset="+charset);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setReadTimeout(5000); // 2 second timeout.
String query = String.format("Email=%s&Password=%s",
URLEncoder.encode(username, charset),
URLEncoder.encode(password, charset));
OutputStream output = null;
try {
output = connection.getOutputStream();
output.write(query.getBytes(charset));
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
connection.getInputStream();
List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
if (cookies == null)
throw new IOException();
for (String cookie : cookies)
{
if (cookie.startsWith("authcookie"))
return cookie; // this is the only correct path out.
}
throw new IOException();
}