httpRequest.getCookies() is not fetching all cookies in safari browser alone, but we getting in the android/web application, we have using the hybrid app and web application. hybrid app ioS screen render from safari, it is not fetching all cookies
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
String cookieName = cookie.getName();
String cookieValue = cookie.getValue();
}
}
return cookies;
Related
I have successfully opened the blackberry access from our IOS app using the url scheme access://open? , but it seems to be not working on Android. Our application is not integrated with blackberry sdk .
For anyone who needs it you can open the blackberry access from your app using the blackberry access appid com.good.gdgma.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(urlString));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setPackage("com.good.gdgma");
try {
getApplicationContext().startActivity(intent);
}
catch(ActivityNotFoundException ex) {
showNotInstalledDialog(App.BLACKBERRY_ACCESS);
}
There is a setting the in application configuration policy for BlackBerry Access that enables or disables this feature. It's called "Allow external apps to open HTTP/HTTPS URLs through BlackBerry Access" and is set for the app config for BlackBerry Access within BlackBerry UEM. This setting applies to all non BlackBerry Dynamics methods of opening BlackBerry Access.
If you were to integrate with the BlackBerry Dynamics SDK the recommended method is to use the BlackBerry Dynamics Shared Services framework to call the Open HTTP URL Service. It's available for both iOS and Android. Here's some Android sample code to use it.
Here's a code snippet that does just that:
private static final String SERVICE_ID = "com.good.gdservice.open-url.http";
private static final String SERVICE_VERSION = "1.0.0.0";
private static final String ACCESS_ENTITLEMENT_ID = "com.good.gdgma";
private static final String HTTP_OPEN_URL_SERVICE_METHOD_NAME = "open";
....
//Get the service providers for the Open HTTP URL service.
List<GDServiceProvider> providers = GDAndroid.getInstance().getServiceProvidersFor(SERVICE_ID, SERVICE_VERSION,
GDServiceType.GD_SERVICE_TYPE_APPLICATION);
//Ensure an provider of the Open HTTP URL service was found.
if(providers == null || providers.size() == 0)
{
//No providers found.
showError("No Open HTTP URL were found.");
}
else
{
boolean foundAccess = false;
String yourURL = "www.whereEverYouWantToGo.com";
for (int count = 0; count < providers.size(); count++)
{
GDServiceProvider provider = providers.get(count);
//Ensure BlackBerry Access was found.
if (provider.getIdentifier().equalsIgnoreCase(ACCESS_ENTITLEMENT_ID))
{
foundAccess = true;
String address = providers.get(count).getAddress();
Map<String, Object> params = new HashMap<>();
params.put("url", yourURL);
try
{
//Launch BlackBerry Access.
GDServiceClient.sendTo(address, SERVICE_ID, SERVICE_VERSION,
HTTP_OPEN_URL_SERVICE_METHOD_NAME, params, null,
GDICCForegroundOptions.PreferPeerInForeground);
} catch (GDServiceException e)
{
showError(e.toString());
}
}
}
if (!foundAccess)
{
showError("BlackBerry Access not found.");
}
}
I am overriding this method for my WebView. And I am using the proper domain. When debugging using an Android simulator I only see a partial list of all of the cookies. The one in particular I'm requiring the value of isn't listed. Any suggestions? Source code below.
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
_cookieManager.Flush();
string customerId = null;
var cookies = _cookieManager.GetCookie(Constants.DCH_DOMAIN);
Log.Info(TAG, string.Format("Cookies retrieved as {0}.", cookies));
if (cookies != null)
{
string[] tempList = cookies.Split(';');
foreach (var pair in tempList)
{
if (pair.Contains("wishlist_customer_id"))
{
string[] tempPair = pair.Split('=');
customerId = tempPair[1];
if (customerId != null)
{
Log.Info(TAG, string.Format("Customer ID retrieved as {0}.", customerId));
PCLStorage(customerId);
}
}
}
}
UPDATE There is another domain that stores cookies as part of this web session. No different in path, HTML, same site, secure, etc. between the four cookies. I'll attach what Chrome on my workstation looks like, showing these four cookies. Then I'll attach what my Android simulator's device log looks like, where I output the cookie collection based on the subclassed WebViewClient. Only two of the four cookies appear to be there. I inserted a SystemClock.Sleep(5000) before retrieving the cookies, in order to give them a chance to fully populate as well.
Chrome session
WebViewClient session
you should do cookied synchronization before load url to ensure complete cookie data like :
_cookieManager.flush ();
webView.loadUrl(url);
then get cookies int the OnPageFinished method :
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
string customerId = null;
var cookies = _cookieManager.GetCookie(url);
Log.Info(TAG, string.Format("Cookies retrieved as {0}.", cookies));
if (cookies != null)
{
string[] tempList = cookies.Split(';');
foreach (var pair in tempList)
{
if (pair.Contains("wishlist_customer_id"))
{
string[] tempPair = pair.Split('=');
customerId = tempPair[1];
if (customerId != null)
{
Log.Info(TAG, string.Format("Customer ID retrieved as {0}.", customerId));
PCLStorage(customerId);
}
}
}
}
Thanks to Leo Zhu the issue has been resolved. Apparently before I load the WebView URL I needed to flush the CookieManager instance. This resulted in all of the cookies for this particular resource to appear and be available for querying. Hope that this might help anyone else who runs into a similar challenge!
I want to integrate Android application with Dynamics CRM 2015 Online and On-Premise.
For online version Connect Android App to Dynamics CRM using Web API this works fine, But ADAL dependency is not supported for OnPremise.
Are there any resources which show the basic steps to access the Microsoft CRM on-premise.
Any sample code around same for connecting to REST endpoint will be helpful.
Setup IFD for your on-premise deployment.
Authenticate to Microsoft Dynamics 365 with the Web API
When you use the Web API for Dynamics 365 (online) or an on-premises
Internet-facing deployment (IFD) you must use OAuth as described in
Connect to Microsoft Dynamics 365 web services using OAuth.
Connect to Microsoft Dynamics 365 web services using OAuth
Applies To: Dynamics 365 (online), Dynamics 365 (on-premises),
Dynamics CRM 2016, Dynamics CRM Online
The recommended authentication API for use with the Dynamics 365 Web
API is Azure Active Directory Authentication Library (ADAL), which is
available for a wide variety of platforms and programming languages.
The ADAL API manages OAuth 2.0 authentication with the Dynamics 365
web service identity provider. For more details on the actual OAuth
protocol used, see Use OAuth to Authenticate with the CRM
Service.
ADFS is required for on prem. Check this documentation for how to set up the servers. https://msdn.microsoft.com/en-us/library/dn531009(v=crm.7).aspx
If this is a one off project for a client it may also be worth creating a .Net based proxy between the app and CRM. That way you can use the .Net SDKs without worrying about losing support
On-Prem using ADAL:
public static String GetAdfs(String url) throws IOException,
ParserConfigurationException, SAXException {
URL WsdlURL = new URL(url
+ "/XrmServices/2011/Organization.svc?wsdl=wsdl0");
HttpURLConnection rc = (HttpURLConnection) WsdlURL.openConnection();
rc.setRequestMethod("GET");
rc.setDoOutput(true);
InputStreamReader read = new InputStreamReader(rc.getInputStream());
StringBuilder sb = new StringBuilder();
int ch = read.read();
while (ch != -1) {
sb.append((char) ch);
ch = read.read();
}
String response = sb.toString();
read.close();
rc.disconnect();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document x = builder
.parse(new ByteArrayInputStream(response.getBytes()));
NodeList nodes = x.getElementsByTagName("ms-xrm:Identifier");
if (nodes.getLength() == 0)
return null;
return nodes.item(0).getFirstChild().getTextContent()
.replace("http://", "https://");
}
// ADAL init
AuthenticationContext authenticationContext = new AuthenticationContext(LoginActivity.this, GetAdfs(url), false);
authenticationContext.acquireToken(context, domain, Constants.CLIENT_ID, Constants.REDIRECT_URL, "", PromptBehavior.Auto, "", callback);
private AuthenticationCallback<AuthenticationResult> callback = new AuthenticationCallback<AuthenticationResult>() {
#Override
public void onError(Exception exc) {
ViewHelper.showToast(context, "Domain name or user not available in ms crm");
}
#Override
public void onSuccess(AuthenticationResult result) {
if (result == null || result.getAccessToken() == null || result.getAccessToken().isEmpty()) {
Toast.makeText(context, "Token is Empty", Toast.LENGTH_SHORT).show();
} else {
Log.i(Keys.TOKEN_KEY, result.getAccessToken());
}
}
};
1. On-Prem and Online using Soap
2. On-Prem and Online using ADAL
In my android application I have a webview. It loads URLs from multiple domains. I need to delete all cookies from a specific domain. I want to keep cookies from other domains. But I need to delete all cookies from one domain. I'm open to all other solutions that handles my request. (note that domain uses both http and https)
But when I try to use CookieManager.setCookie, all available cookies for that domain didn't deleted. Multiple cookie keys appeear when I try to write to that keys.
I attach my code below. You can find results in comment lines. At the end of story I get this cookie. Note for multiple values:
"userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"
My helper function that splits cookie string to get cookie keys:
public static Vector<String> getCookieAllKeysByCookieString(String pCookies) {
if (TextUtils.isEmpty(pCookies)) {
return null;
}
String[] cookieField = pCookies.split(";");
int len = cookieField.length;
for (int i = 0; i < len; i++) {
cookieField[i] = cookieField[i].trim();
}
Vector<String> allCookieField = new Vector<String>();
for (int i = 0; i < len; i++) {
if (TextUtils.isEmpty(cookieField[i])) {
continue;
}
if (!cookieField[i].contains("=")) {
continue;
}
String[] singleCookieField = cookieField[i].split("=");
allCookieField.add(singleCookieField[0]);
}
if (allCookieField.isEmpty()) {
return null;
}
return allCookieField;
}
I get present cookies:
// I take cookie string for specific URL
mCookieManager = CookieManager.getInstance();
String url2="https://mysite.com";
String cookieString = mCookieManager.getCookie(url2);
Toast.makeText(mContext, "cookie string:\n"+cookieString, Toast.LENGTH_SHORT).show();
// result is: userid=12%34; token=12ased; remember_check=0;
Then I call to replace old cookies.
Vector<String> cookie = CookieUtil.getCookieAllKeysByCookieString(cookieString);
if (cookie == null || cookie.isEmpty()) {
Toast.makeText(mContext, "cookie null", Toast.LENGTH_SHORT).show();
}
if (cookie != null) {
int len = cookie.size();
Toast.makeText(mContext, "cookie number: "+len, Toast.LENGTH_SHORT).show();
// result is, cookie number: 3
String cookieNames="";
for (int i = 0; i < len; i++) {
cookieNames += "\n"+cookie.get(i) ;
mCookieManager.setCookie(url2, cookie.get(i) + "='-1';");
}
Toast.makeText(mContext, "cookieNames:\n"+cookieNames, Toast.LENGTH_SHORT).show();
// result is: "cookienames: userid token remember_check"
mCookieSyncManager.sync();
cookieString = mCookieManager.getCookie(url2);
Toast.makeText(mContext, "cookie string:\n"+cookieString, Toast.LENGTH_SHORT).show();
mCookieSyncManager.sync();
// result is: "userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"
}
Edit:
I also tried setCookie like this:
mCookieManager.setCookie(url2, cookie.get(i) + "=-1;");
mCookieManager.setCookie(url2, cookie.get(i) + "=-1");
Edit2: setCookie's signature is like this:
/**
* Sets a cookie for the given URL. Any existing cookie with the same host,
* path and name will be replaced with the new cookie. The cookie being set
* must not have expired and must not be a session cookie, otherwise it
* will be ignored.
*
* #param url the URL for which the cookie is set
* #param value the cookie as a string, using the format of the 'Set-Cookie'
* HTTP response header
*/
public void setCookie(String url, String value) {
throw new MustOverrideException();
}
Although I get same keys inside cookie string ("userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';") will they have different host or path ?
I've had a similar experience with the CookieManager in Android. Setting the same cookie will indeed add it as a new cookie.
Please try to implement this solution. It will enable you to flush the cookies you want to remove and then you'll be able to set the again as you desire.
Good luck!
First, we can delete the cookie via CookieManager's interfaces:
setCookie(URL, 'COOKIE_KEY=;');
Then, we need to find out the correct URL, considering both Domain and Path attributes of the cookie.
For example, the following cookie
document.cookie = 'COOKIE_NAME=COOKIE_VAL; path=/; domain=.example.com;'
can be deleted by
setCookie('.example.com', 'COOKIE_NAME=;')
and cannot be deleted by
setCookie('www.example.com/info.html', 'COOKIE_NAME=;')
Finally, here is an example to remove a cookie.
String[] kvPairs = CookieManager.getInstance().getCookie(url).split(" ");
for (String kvPair : kvPairs) {
String newPair = kvPair.replaceAll("=.*", "=;");
// Delete the cookie asynchronously.
CookieManager.getInstance().setCookie(url, newPair);
}
I've been pulling my hair trying to figure this out: I'm making an HttpsURLConnection and using java.net.cookiemanager to manage my cookies (there's no way of using android.webkit.cookiemanager to HttpUrlConnection/HttpsUrlConnection as I have understood?). I need to save my longtime cookie to later connections.
Sadly I can't use http://loopj.com/android-async-http/ and it's PersistentCookieStore because I need to allow an untrusted certificate (using http://abhinavasblog.blogspot.se/2011/07/allow-untrusted-certificate-for-https.html). I've tried using their PersistentCookieStore alone but they are using apache cookies and I'm using java.net cookies...
This is what I've tried:
cManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
private void setSharedPreferences(){
List<HttpCookie> cookies = cManager.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.d(tag,"no cookies received");
} else {
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().equals("rememberMe")) {
editor.putString(
"rememberMe", cookies.get(i).toString());
editor.commit();
}
}
}
}
And when I'm retrieving the cookie on next app launch:
SharedPreferences sharedPreferences = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
String rememberString = sharedPreferences.getString("rememberMe", "none");
if (!rememberString.equals("none")) {
Log.d("rememberME är inte", "none!");
URI uriToCookie = null;
try {
uriToCookie = new URI("https://myservername.com");
} catch (URISyntaxException e) {
e.printStackTrace();
}
List<HttpCookie> cookieList = HttpCookie.parse(rememberString);
cManager.getCookieStore().add(uriToCookie, cookieList.get(0));
}
The cookie is added to cManager but is not recognized by the server.. I believe there is some sort of parse problem. Anyone got the solution?
I used this part:
cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
Log.v("Cookie:", cookies.toString());
if (cookies.isEmpty()) {
} else {
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().contentEquals("PHPSESSID")) {
PHPSESSID = cookies.get(i).getValue();
}
}
}
Just use the contentEquals to get the domain name and let it match with urs and store it. Oh and i used PHPSESSID as a string which i dumped in my sharedprefs for later
After looking at your code, I code be wrong but you are simply not storing the entire cookie:
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().equals("rememberMe")){
editor.putString("rememberMe", cookies.get(i).toString());
editor.commit();
}
}
You get the List length of cookies with cookies.size() and loop and get all the cookies but you commit the save key-value "rememberMe" instead of appending or storing in separate keys. So basically, you are simply overwriting what you stored over and over.