0

I am trying to access a webpage after successfully logging in, where i get the username and password (from the EditTexts in my app user = (EditText) findViewById(R.id.user); and pass = (EditText) findViewById(R.id.pass); ) and POST it to server.

The problem is that the webview shows the text "Authorization Required.... " (after logging in successfully) only on the phones/tablets that have android 4.2.2 but on 4.4.2, 4.4.4 and 5.0.1 works fine (shows the page intended).

I am running out of ideas and need help.

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", user.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password", pass.getText().toString()));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
httppost.addHeader("Authorization", "Basic BLA123BLA23BLA32343LSJ");

 // Execute HTTP Post Request    
 response = httpclient.execute();

if (response.getStatusLine().getStatusCode() == 200) {
//WebView                   
    webview_asd = new WebView(MainScreen.this);
    webview_asd = (WebView) findViewById(R.id.webview_asd);
    webview_asd.getSettings().setLoadsImagesAutomatically(true);
    webview_asd.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webview_asd.setWebViewClient( new WebViewClient() );
    webview_asd.getSettings().setJavaScriptEnabled(true);


    CookieSyncManager.createInstance (webview_asd.getContext());

    cookies = ((AbstractHttpClient) httpclient).getCookieStore().getCookies();

    if(cookies != null)
    {
        for(Cookie cookie : cookies)
        {
       cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();                        
       CookieManager.getInstance().setCookie(cookie.getDomain(), cookieString);  
        }
    }

    CookieSyncManager.getInstance().sync();

webview_asd.loadURL(URL);   
webview_asd.setVisibility(View.VISIBLE);

( webview shows - "Authorization Required. This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials(e.g., bad password), or your browser doesn't understand how to supply the credentials required." )

enter image description here

Later edit: Just figured out that 4.2.2 doesn't support HTML5 and that's why i got this problem.

Vlad.mir
  • 685
  • 1
  • 10
  • 28
  • You have not very well explained how your code should work. Please try again. – greenapps Jun 18 '15 at 17:56
  • What did you add? You still did explain nothing. You did not even mention cookies. You do something very special with them. You login with http and after that use them in a webview. Now please explain in detail. – greenapps Jun 19 '15 at 07:48
  • @greenapps I used the cookies standard, didn't thinked i do something very special. The code is simple: a basic httpPost, added a Header, catch the response if it's 200 than load and show the webview and implement the cookies. I'm new to android and i hope i explained it good enough and if it's anything to add i'm more than welcome to learn more. – Vlad.mir Jun 19 '15 at 08:25

1 Answers1

0

I would guess that the webpage required Basic Authentication. Use WebViewClient callback onReceivedHttpAuthRequest. This post shows a good basic example.

Community
  • 1
  • 1
poisondminds
  • 427
  • 4
  • 8
  • I have tried your method but no changes. I think i'm going to try to implement another sort of webview. – Vlad.mir Jun 23 '15 at 11:37