I have a Windows Phone 7 application that is utilizing the DataServicesClient to access a WCF DataService (ODATA) and I want to ask the service to compress the response.
I know if the Request Headers includes "Accept-Encoding" header with the value of "gzip" the WCF DS will compress the response.
Is there a way to add this header to the Request when using the DataServicesClient on WP7? The Request Headers collection appears to be readonly. What I would like to be able to do is the following (but the Headers property does not have an Add method in this context.)
void entities_SendingRequest(object sender, SendingRequestEventArgs e) { e.Headers.Add("Accept-Encoding", "gzip"); }
Michael
You should be able to modify header collection with following code
private void OnSendingRequest(object sender, System.Data.Services.Client.SendingRequestEventArgs e)
{
e.RequestHeaders["Accept-Encoding"] = "gzip";
}
This looks a lot like your other active question ;)
As mentioned on that question, it looks like the ODATA client has no way for you to modify the headers of a request before it is sent, though you could make a feature request.
Related
I am looking for a way to retrieve sharing permissions on specific entities.
Is there a way to do this via the web api?
I am aware of RetrieveSharedPrincipalsAndAccessRequest but since the rest of my tool relies only on the web api i would like to avoid using Organization Service completely.
This request is available in version 9.0 (Dynamics 365 july update)
I believe that your are using 8.x version. In such case this is not that simple but also doable. Simply create a custom Action:
https://msdn.microsoft.com/en-us/library/dn481600.aspx
Actions can be used from workflows, plugins and, what is most useful in this scenario, directly called from WebAPI:
https://msdn.microsoft.com/en-us/library/mt607600.aspx
So for example your call would look like that:
POST [Organization URI]/api/data/v8.2/new_RetrievePrincipalAccessAction HTTP/1.1
Accept: application/json
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
{
"ObjectId": 3,
"ObjectType": "account"
}
Now, you need to create a plugin and register it in Post-Operation of your action (actions generate a custom message that you can use to register plugins). For example:
https://community.dynamics.com/crm/b/magnetismsolutionscrmblog/archive/2017/09/18/how-to-trigger-plugins-on-custom-messages-using-actions-in-dynamics-365
In your plugin, you can of course call the RetrieveSharedPrincipalsAndAccessRequest using standard IOrganizationService and simply put the result in OutputParameters of an action. Most useful would be JSON string:
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
//do the logic retrieve what you want
context.OutputParameters["result"] = someResultAsJsonString;
}
Your JS webAPI call will return
{
result: someResultAsJsonString
}
And you can do whatever you want with this in your JS code :)
I am doing an upload via CORS to Amazon S3 with the Kendo Upload control. I'm having an issue with the fact that I need to grab a signature from my server, then add it to the 'data' for the event object of 'upload' handler I created. The problem is, of course, that in the handler I fire off an async request to get the signature, and the upload handler continues on it's merry way without the signature data i need. The published API has no 'upload()' or something command that I could call when my async request returns.
I saw an ASP-Kendo-S3 example somewhere, but it's not exactly clear from that code, how that signature is being obtained, and of course, I'm not using ASP.
Kendo Upload has an onUpload event. In Kendo's asp.net example there really isn't anything specific to that framework that wouldn't port to anything else.
They populate the page initially with the profile (base64 encoded JSON).
To get a signature for that base64 encoded json profile they use this method (C#):
private static string Sign(string text, string key)
{
var signer = new HMACSHA1(Encoding.UTF8.GetBytes(key));
return Convert.ToBase64String(signer.ComputeHash(Encoding.UTF8.GetBytes(text)));
}
It looks pretty self explanatory to the point where you could port it to another language.
In the app I'm working on, I want to be able to open the webbrowser and navigate to a specific URL.
This is easy enough using the WebBrowserTask, but the URL I want to reach requires a couple of request headers.
I haven't been able to figure out how to achieve this, and is of course hoping someone here can point me in the right direction, or even show an example.
If the task it self doesn't support headers, is there any other way?
EDIT:
Well I found out that the WebBrowserTask in its current state does not support adding headers.
My solution was to make a new XAML page in the app and add a WebBrowser control as the only object.
The webbrowser control supports adding headers like this:
Uri uri = new Uri("http://YOUR_URL.COM");
string headers = String.Format("HEADER1:{0}\r\nHEADER2:{1}\r\nHEADER3:{2}\r\n", projectId, username, password);
webBrowser.Navigate(uri, null, headers);
The important part of adding the headers is to remember to seperate the headers with \r\n
I am building an app which is already build in Android & other Mobile Platform. Since the App uses REST based Webservices build in JAVA, thus I need to use these Webservice URL. The code uses HttpClient and HttpGet for GET,POST,PUT & DELETE operation in Android . Can anyone guide me where to start with as I am new to this Platform.
You can use HttpWebRequest ( http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.95).aspx ) to make calls to a REST service
I would recommend using the WebClient class for simple HTTP based communication. Here is the basic format I typically use when making a request to a web service:
WebClient web = new WebClient();
web.OpenReadCompleted += new OpenReadCompletedEventHandler(RequestComplete);
web.OpenReadAsync(new Uri("http://fullurlofyourwebservice.com"));
You can then write a method for the RequestComplete method referenced in the second line of code:
void RequestComplete(object sender, OpenReadCompletedEventArgs e)
{
string response = "";
using (var reader = new StreamReader(e.Result))
{
response = reader.ReadToEnd();
}
}
You can then process the response as a simple string, or do something like XDocument.Parse(response) if your response is in XML format.
Check out the full MSDN documentation for a complete reference.
I've recently started using RestSharp. http://restsharp.org/
Small, simple and does what it says on the box.
HI, is possible to add request headers in WP7 WebBrowser control?
There is no way to do this. If you need to change headers you'll need to use HttpWebRequest.
You could intercept the requests from the WebBrowser control and make them yourself via HWR but this could get complicated very quickly.
No - I don't think there's any API hook available for this.
It's a similar problem to the "change the user agent" request discussed in Bring back mobile version of website in WebBrowser control for wp7?
Sorry to necro but the answers here are wrong. Headers can be added to a WebBrowser through the Navigate method.
WebBrowser.Navigate(YourURI, null, YourCustomHeaderString)
See this page: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff626636(v=vs.105).aspx
.
These headers will only apply to the first page navigated to through your code. If you want the headers to stay the same even when users click a link inside the web browser control, add this for the WebBrowser's navigating event:
private void browser_Navigating(object sender, NavigatingEventArgs e)
{
string url = e.Uri.ToString();
if(!url.Contains("YESHEADERS"))
{
e.Cancel = true;
string newUrl;
if(url.Contains("?"))
{
newUrl = url + "&YESHEADERS";
}
else
{
newUrl = url + "?YESHEADERS";
}
browser.Navigate(newUrl, null, "fore:" + Variables.GetForeground() + "")
}
}
Here's what that does:
We create an indicator, YESHEADERS, that tells us whether or not we have added custom headers.
When the WebBrowser tries to Navigate, we check whether or not the URL it is navigating to, e.Uri, contains YESHEADERS.
If it does, we've already added our headers. Take no action
If it does not, cancel the current navigation. Create a new URL equal to the old URL plus our indicator. We add YESHEADERS on to the new URL in it's query string. If you are not familiar with query strings that is fine, just know that they are extra strings on the URL that have no effect in our case. About Query Strings
Then, we navigate to the new URL, and add our custom headers.
In short, if we have our indicator YESHEADERS the web browser knows that we've added our custom headers, if we don't have YESHEADERS, than the web browser needs to add the headers.