(UWP) WebClient and downloading data from URL in - windows

How do you download data from an url in UWP? I'm used to using WebClient's DownloadData-method, but it can't be used anymore.

.NET for UWP does not have the WebClient class.
But you have several alternative ways to download the data from URL in UWP.
For example:
var request = WebRequest.CreateHttp("http://www.bing.com");
var donnetClient = new System.Net.Http.HttpClient();
var winrtClient = new Windows.Web.Http.HttpClient();
If you want to download the data at background, you can use the BackgroundDownloader class

Related

How to download file in Xamarin.Forms that are in Base64 format?

I am creating an application that download and upload attachment from the Xamarin.Forms application.
I am using .Net standard project and try to use with the "CrossDownloadManager" Nuget but it is not compatible and other reference for download file are using url but in this situation I have base64byte string.
I also try with This reference but it doesn't work for me.
Application works on Android and iOS.
I attached sample response Here:
Can anyone look into this and suggest me what should I have to do in that?
download a file given a url
var client = new HttpClient();
var data = await client.GetStringAsync(url);
if data is Base64, decode it
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
var decoded = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
save decoded string
File.WriteAllText(filepath, decoded);

FireFox Addon WebExtension API - open local file / application

I would like to write a mozilla firefox extension by using the WebExtension API. I couldn´t find a source code using the WebExtension API for my purposes.
var {Cc, Ci} = require("chrome"); // Low-Level API Imports (For Launcher)
var prefs = require("sdk/simple-prefs").prefs;
var app = "C:\\abcd\\test.exe";
var file = Cc["#mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
file.initWithPath(app);
var process = Cc["#mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
if (file.exists()) {
process.init(file);
var params = prefs["param"];
var args = ["" + params + ""];
process.run(false, args, args.length);
}
How does a source code for writing a mozilla firefox extension by using the WebExtension API look like?
Unfortunately I can´t use your suggested solution, because there have to be made settings on the local PC additional to the Addon. I would like to prevent making these settings. I am interested in a solution, where a variable path can be executed directly out of the Browser. For example, a folder or a local file should open there
can't be done with webextensions alone (webextensions were in part meant to prevent this), you'd have to have a native app installed as well, and message pass to it, using the native messaging api that was mentioned.

Does Google offer API access to the mobile friendly test?

Is there an API that allows access to Google's Mobile Friendly Test which can be seen at https://www.google.com/webmasters/tools/mobile-friendly/?
If you can't find one by googling, it probably doesn't exist.
A hacky solution would be to create a process with PhantomJS that inputs the url, submits it, and dirty-checks the dom for results.
PhantomJS is a headless WebKit scriptable with a JavaScript API.
However, if you abuse this, there is a chance that google will blacklist your ip address. Light use should be fine. Also be aware that google can change their dom structure or class names at any time, so don't be surprised if your tool suddenly breaks.
Here is some rough, untested code...
var url = 'https://www.google.com/webmasters/tools/mobile-friendly/';
page.open(url, function (status) {
// set the url
document.querySelector('input.jfk-textinput').value = "http://thesite.com";
document.querySelector('form').submit();
// check for results once in a while
setInterval(function(){
var results = getResults(); // TODO create getResults
if(results){
//TODO save the results
phantom.exit();
}
}, 1000);
});
There is an option in pagespeed api
https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url={url}&key={api key}
key can be obtained form google cloud platform.
Acquire a PageSpeed Insights API KEY in https://console.developers.google.com/apis/api/pagespeedonline-json.googleapis.com/overview?project=citric-program-395&hl=pt-br&duration=P30D and create a credentials, follow the google's instructions.
In C# (6.0) and .NET 4.5.2, I did some like this:
(add in your project a reference for Newtonsoft.Json.)
String yourURL = "https://www.google.com.br";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://www.googleapis.com");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync($"/pagespeedonline/v3beta1/mobileReady?url={yourURL }&key=AIzaSyArsacdp79HPFfRZRvXaiLEjCD1LtDm3ww").Result;
string json = response.Content.ReadAsStringAsync().Result;
JObject obj = JObject.Parse(json);
bool isMobileFriendly = obj.Value<JObject>("ruleGroups").Value<JObject>("USABILITY").Value<bool>("pass");
There is an API (Beta) for the Mobile Friendly-Test. (Release Date: 31.01.2017).
The API test outputs has three statuses:
MOBILE_FRIENDLY_TEST_RESULT_UNSPECIFIED Internal error when running this test. Please try running the test again.
MOBILE_FRIENDLY The page is mobile friendly.
3.NOT_MOBILE_FRIENDLY The page is not mobile friendly.
Here are more informations: https://developers.google.com/webmaster-tools/search-console-api/reference/rest/v1/urlTestingTools.mobileFriendlyTest/run

Self Hosted ASP.NET Web API: how to embed images?

I switched my ASP.NET Web API from IIS-hosted to self-hosted. So far I had my images deployed in its own folder (and accessed them with HostingEnvironment.MapPath). Obviously this folder doesn't exist in a self hosted environment. How can I handle images instead?
OK, I figured it out. Here's what I did:
set Build Action of each image as Embedded Resource
replace my MapPath with the following piece of code:
var resourcePath = "My.Namespace." + iconPath; //iconPath = subfolder.subfolder.file.ext
using (Stream imageStream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream(resourcePath))
{
...

HttpClient and HttpGet Support in WP7

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.

Resources