WinRT: downloading file - windows-runtime

I want to download file from server and show the progress of loading. How may I do this?
For downloading I use this:
HttpClient client = new HttpClient();
Stream response = await client.GetStreamAsync(url);
But I don't know how to get size of downloading file and don't know how much it was downloaded
Thanks for any help

HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync(url);
There is StatusCode property in response object, you can analyze this property whether you get OK or PartialContent.
You can also watch the following links for further details:
http://social.msdn.microsoft.com/Forums/en/winappswithcsharp/thread/f27c385a-e783-4062-a0b8-e21bf1704f95
http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh781241.aspx

Related

Xamarin App Sends Json Data When Phone Virtual But Doesn't Send Physical

I don't know where to look or what to check but ask my question in title. Xamarin forms app works when sending json data over virtual phone instance but doesn't send data over physical no matter which platform i use , it is the same with iOS and Android.
static async Task phoneInfo()
{
string url = "http://blabla.com/api/blabla";
string sContentType = "application/json";
JObject jsonObject = new JObject();
jsonObject.Add("DeviceModel", DeviceInfo.Model);
jsonObject.Add("DeviceManufacturer", DeviceInfo.Manufacturer);
jsonObject.Add("DeviceName", DeviceInfo.Name);
jsonObject.Add("DeviceVersion", DeviceInfo.VersionString);
jsonObject.Add("DevicePlatform", DeviceInfo.Platform);
jsonObject.Add("DeviceIdiom", DeviceInfo.Idiom);
jsonObject.Add("DeviceType", DeviceInfo.DeviceType.ToString());
jsonObject.Add("AreaOne", DateTime.UtcNow.ToString());
jsonObject.Add("Deleted", false);
HttpClient oHttpClient = new HttpClient();
var oTaskPostAsync = await oHttpClient.PostAsync(url, new StringContent(jsonObject.ToString(), Encoding.UTF8, sContentType));
}
usage is simple like code. just put await phoneInfo(); where i want to take info.
i have accesswifistate and internet permission over Android and NSAppTransportSecurity for non https connection with iOS.
Any ideas where am i doing wrong?

Extracting gzip data from Apache-httpclient without decompression

I'm using apache httpclient 4.3.5 to send a request to an upstream server which returns a gzipped response. I need to pass this response AS-IS to a downstream server without any form of decompression. However, httpclient is far too helpful and insists on decompressing the response and I can't find any way of persuading it to stop.
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse serverResponse = client.execute(serverRequest);
try {
HttpEntity entity = serverResponse.getEntity();
downstreamResponse.setStatus(serverResponse.getStatusLine().getStatusCode());
for (Header header : serverResponse.getAllHeaders()) {
downstreamResponse.setHeader(header.getName(), header.getValue());
}
entity.writeTo(downstreamResponse.getOutputStream());
downstreamResponse.flushBuffer();
} finally {
serverResponse.close();
}
I'm sure that there is some way of configuring the client using some form of the construct
return HttpClients.custom()
....
.build();
but I can't find it. Can the experts please advise?
CloseableHttpClient client = HttpClients.custom()
.disableContentCompression()
.build();

WinRT use HttpClient to call Web API based URL with token

We are building a WinRT app which gets data from server which is Web API based & so it gives data in json and/or XML format.
When app user logs in for the first time using his credentials(username,password), the response that comes from server is a success bit & a TOKEN, which should be used in successive URL requests.
I am using httpclient for sending requests
using (HttpClient httpClient1 = new HttpClient())
{
string url = "http://example.com/abc/api/process1/GetLatestdata/10962f61-4865-4e7a-a121-3fdd968824b5?employeeid=6";
//The string 10962f61-4865-4e7a-a121-3fdd968824b5 is the token sent by the server
var response = await httpClient1.GetAsync(new Uri(url));
string content = await response.Content.ReadAsStringAsync();
}
Now the response that i get is with status code 401 "unauthorised".
And the xml i get in response is "Unauthorised User".
Is there anything i need to change in appManifest??
I've checked this, but cant we use httpclient without credentials??
Your Capabilities are enough. You don't even need Internet (Client) because it's included in Internet (Client & Server).
You do not have credentials for WinRT HttpClient, in your linked post they referr to System.Net.Http.HttpClientHandler.
Maybe you can use the HttpBaseProtocolFilter to add the credentials?
using (var httpFilter = new HttpBaseProtocolFilter())
{
using (var httpClient = new HttpClient(httpFilter))
{
httpFilter.ServerCredential...
}
}
I don't know your security mechanism, I'm using a HttpClient and my session-key is in a cookie. But I think your client code looks fine.

BackgroundTransferRequest DownloadLocation to get the Response content

I am uploading a file to the server using BackgroundTransferRequest object. I need to access the response header sent from the server. Based on the research, I understand that there is no direct access to the Response of the request and one possible work around is to provide a download location to which the response content will be written.
However, when I try to do that, there is no data written to that file and its 0 bytes. My request server is actually sending a response having ONLY the headers (without any content).
So my question is, does the response have to have body in order to get the response written to the DownloadLocation?
My code sample:
var request = new BackgroundTransferRequest(targetUri)
{
DownloadLocation = new Uri(downloadTo, UriKind.Relative),
UploadLocation = new Uri(uploadFrom, UriKind.Relative),
Method = "POST"
};

Json: the remote server returned an error (500) internal server error

i am trying to read a web service in Json format
here is my code:
WebClient wc = new WebClient();
wc.UseDefaultCredentials = true;
var data = wc.DownloadString(JsonUri);
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(data));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<PaymentMethod>));
var result = serializer.ReadObject(ms);
ms.Close();
ms.Dispose();
i get an error on this line:
var data = wc.DownloadString(JsonUri);
the JsonUri is : http://avaris.kwekud.com/api/v1/items/uniqueitem/?username=joel&api_key=959dd41efd06b84ca7f10b1b12f5f3e6567c07dc&format=json
Any Help
thanks
It looks like there is a problem in the python code on the server that is returning the API. A good idea is to try to make the call from outside of your code to make sure that it is not your code. Any packet sniffing tool eg firebug in firefox will be able to tell you if the call is going wrong pr its your code being badly formed.
It looks like the API is currently broken, so you should put a try ... catch in there and handle the exception for situations like this: