Android uygulamasında bir sunucu tarafından sağlanan bazı web eşleme verilerine erişmek için bir web görünümü kullanıyorum. Sunucu, bu verilere erişime izin vermek için bazı HTTP form tabanlı kimlik doğrulaması gerektirir. Sitenin bir mobil sürümünün olmaması nedeniyle, giriş sayfasını (veya başka bir sayfayı) görüntülemek oldukça kötü görünüyor. Aşağıdaki yaklaşımın düşündüm bu yüzden ne yazık ki site benim ulaşmak içine pek geçerli:Program aracılığıyla WebView'den oturum açın
- düşünce kullanıcı adı ve şifrenizi almasına yerel kullanıcı arabirimini kullanmak bir HTTP sonrası sunucuya bu bilgileri göndermek
- alındığında yanıt sunucu
- web görünümünde nihayet istenen verilere erişmek için
- deneyin için çerezler ayarlayabilir gönderiyor çerezleri aldıktan sonra
Şimdilik sadece giriş aşamasını geçmeye çalışıyorum.
Bu geçerli bir çözüm mü yoksa yanlış mı? Başka bir şey denemeliyim?
Bütünlüğü için ben
A. kimlik doğrulama parçası
private String authenticate() throws Exception
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mySite/login_form");
HttpResponse response = null;
BufferedReader in = null;
String resultContent = null;
try
{
// Add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("came_from", ""));
nameValuePairs.add(new BasicNameValuePair("form.submitted", "1"));
nameValuePairs.add(new BasicNameValuePair("js_enabled", "0"));
nameValuePairs.add(new BasicNameValuePair("cookies_enabled", ""));
nameValuePairs.add(new BasicNameValuePair("login_name", ""));
nameValuePairs.add(new BasicNameValuePair("pwd_empty", "0"));
nameValuePairs.add(new BasicNameValuePair("name", "username"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
// Execute HTTP Post Request
response = httpclient.execute(httppost,localContext);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null)
{
sb.append(line + NL);
}
in.close();
resultContent = sb.toString();
Log.i("mytag","result :"+resultContent);
cookies = new java.util.ArrayList();
cookies = cookieStore.getCookies();
}
catch (ClientProtocolException e)
{
Log.i("mytag","Client protocol exception");
}
catch (IOException e)
{
Log.i("mytag","IOException");
}
catch(Exception e)
{
Log.i("mytag","Exception");
Log.i("mytag",e.toString());
}
return resultContent;
}
B. istenen sayfa
private void init()
{
CookieSyncManager.createInstance(this);
CookieManager cookieMan= CookieManager.getInstance();
cookieMan.setAcceptCookie(true);
cookies = StartupActivity.listAfter;
if(cookies != null)
{
for (int i = 0; i<cookies.size(); i++)
{
Cookie cookie = cookies.get(i);
cookieMan.setCookie("cookie.getDomain()",cookie.getValue());
}
}
CookieSyncManager.getInstance().sync();
webView = (WebView)findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setWebViewClient(new HelloWebViewClient());
}
protected void onResume()
{
super.onResume();
// test if the we logged in
webView.loadUrl("mySite/myDesiredFeature");
}
o sayfa yüklenirken sonuçları çerezleri ayarlama ve yükleme aşağıdaki kodu göndermeden login_page formunun
Büyük ihtimalle aynı şeyi yapardım –