UWP Httpclient postasync in background task with json string - json

I would simply like to post some json to an api from a background task in my UWP app and get back the response to read it out. My background task is constantly failing with
Platform::DisconnectedException ^ at memory location 0x077FEE74.
I tried many ways to get it work with things from the internet but only slightly adjusting the failure. Without the code I can execute the background task perfectly.
Here the code:
public async void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
HttpClient aClient = new HttpClient();
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript"));
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");
StringContent theContent = new StringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Encoding.UTF8, "application/json");
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, theContent);
if (aResponse.IsSuccessStatusCode)
{
Debug.WriteLine("This is outpuuuuuuuuuuuuuut: " + aResponse.ToString());
}
else
{
// show the response status code
String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " – Reason: " + aResponse.ReasonPhrase;
}
_deferral.Complete();
}
The Json I am trying to imitate looks something like this:
{"keep_login":null,"id":null,"username":"zumbauser","password":"zumbapw"}
Any help is appreciated!

It would be preferrable to use windows.web.http.httpclient for UWP apps since it supports wide range of Languages. See table from the reference
Now for StringContent. Windows.Web.HttpClient as HttpStringContent which is similar to StringContent in System.Net.Http.HttpClient
This is a snippet for example but make sure you read the reference.
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");
HttpStringContent content = new HttpStringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
var client = new HttpClient();
HttpResponseMessage response = await client.PostAsync(theUri, content);
So to complete your Method it will be
public async void Run(IBackgroundTaskInstance taskInstance)
{
_deferral = taskInstance.GetDeferral();
HttpClient aClient = new HttpClient();
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript"));
Uri theUri = new Uri("https://m.xxxx.com/api/v4/session?expand=account,profile)");
HttpStringContent content = new HttpStringContent("{ \"keep_login\": true, \"id\": null, \"username\": \"zumbauser\", \"password\": \"zumbapw\" }", Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, content);
if (aResponse.IsSuccessStatusCode)
{
Debug.WriteLine("This is outpuuuuuuuuuuuuuut: " + aResponse.ToString());
}
else
{
String failureMsg = "HTTP Status: " + aResponse.StatusCode.ToString() + " – Reason: " + aResponse.ReasonPhrase;
}
_deferral.Complete();
}
Note: I tried the same with one of my APP in a background task and it works fine. only case it failed was when i tried to send huge data i got Not enough memory available error.
Good Luck and Happy Coding.

I know this might sound silly, but which HttpClient are you using? The one from System.Net.Http or the one from Windows.Web.Http? Try switching to the other one, it might help

Related

How to upload image to server (using POST) which return json in Windows Phone 8.1 RT?

I am making an app which can upload image to a server (the server works well), and I use this method to upload my image to it, but when I get the respond from the result, it return a null string, can you explain for me what did I do wrong.
I followed this method: How to upload file to server with HTTP POST multipart/form-data
HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
form.Headers.ContentType = new MediaTypeHeaderValue("multipart/form-data");
byte[] bytes = await Converter.GetBytesAsync(storageFile);
form.Add(new ByteArrayContent(bytes, 0, bytes.Count()), "\"upload-file\"", "\"test.jpg\"");
HttpResponseMessage response = await httpClient.PostAsync("my-url", form);
response.EnsureSuccessStatusCode();
httpClient.Dispose();
string sd = response.Content.ReadAsStringAsync().Result;
Debug.WriteLine("res: " + sd); // this return a null string
The request return like this:
--a81d2efe-5f2e-4f84-83b9-261329bee20b
Content-Disposition: form-data; name="upload-file"; filename="test.jpg"; filename*=utf-8''%22test.jpg%22
����Ivg?�aEQ�.�����(��9%�=��>�C�~/�QG$�֨������(�`������QE��Z��
Can you help me please!
P/s: Here is my convert method
public static async Task<byte[]> GetBytesAsync(StorageFile file)
{
byte[] fileBytes = null;
if (file == null) return null;
using (var stream = await file.OpenReadAsync())
{
fileBytes = new byte[stream.Size];
using (var reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}
}
return fileBytes;
}
This might help
private async Task<string> UploadImage(StorageFile file)
{
HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent content = new StringContent("fileToUpload");
form.Add(content, "fileToUpload");
var stream = await file.OpenStreamForReadAsync();
content = new StreamContent(stream);
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
Name = "fileToUpload",
FileName = file.Name
};
form.Add(content);
var response = await client.PostAsync("my-url", form);
return response.Content.ReadAsStringAsync().Result;
}
Use ByteArrayContent instead of StringContent. That Should work.
And if you are expecting a stream-response you should use ReadAsStreamAsync instaed of ReadAsStringAsync.

Sending a photo to server using httpclient class windows phone 8

i'm trying to send a photo to a server using httpclient class but every time i try i get a 0 byte file , here's my code for sending the image
if (e.ChosenPhoto != null)
{
var fileUploadUrl = Globals.baseUrl + "/laravelProjects/VisWall/public/test2";
var client = new HttpClient();
photoStream.Position = 0;
MultipartFormDataContent content = new MultipartFormDataContent();
content.Add(new StreamContent(e.ChosenPhoto), "image", fileName);
HttpResponseMessage result = new HttpResponseMessage();
await client.PostAsync(fileUploadUrl, content).ContinueWith((postTask) =>
{
try
{
result = postTask.Result.EnsureSuccessStatusCode();
}
catch (Exception exc)
{
MessageBox.Show("errorrrrrr");
}
});
}
i've also checked the length of e.ChoosenPhoto and it's not 0
try this piece of code by using MultipartFormDataContent:
HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new StringContent(token), "token");
var imageForm = new ByteArrayContent(imagen, 0, imagen.Count());
imagenForm.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
form.Add(imagenForm, "image", "nameholder.jpg");
HttpResponseMessage response = await httpClient.PostAsync("your_url_here", form);
response.EnsureSuccessStatusCode();
httpClient.Dispose();
string result = response.Content.ReadAsStringAsync().Result;
You could refer these too:
Uploading image and data as multi part content - windows phone 8
How to upload file to server with HTTP POST multipart/form-data
There are plenty of samples out there, which I've not mentioned here. It would be great if you could give a search before you post here.

sending JSON to server windows phone 8

i'm trying to send JSON string to server using Web Client Class but No result is returned,i've tried Rest Client plugin to make sure that server is returning something, here's my postRequest method `
public static void PostRequest(WebClient webclient,string data,string
url,string header,string method)
{
Uri uri = new Uri(url, UriKind.Absolute);
webclient.Headers[HttpRequestHeader.ContentType] = header;
webclient.UploadStringAsync(uri,method,data);
}
and here is where i invoke this method
string newUserJson="{"User_Name":"yosyos","First_Name":"gfhgas","Last_Name":"jagfshg"}";
wc = new WebClient();
wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wb_UploadStringCompleted);
string url = "http://670b9ada.ngrok.com/laravelProjects/TestVisWall/public/users";
helper.PostRequest( wc, newUserJson, url, "application/json", "POST");
Do not use WebClient, it's discouraged from Microsoft.
Try RestSharp library, in my opinion is a very good one. Add recerense to Restsharp, Nuget method is faster: https://www.nuget.org/packages/RestSharp
RestClient client = new RestClient("baseurl");
var request = new RestRequest("/path/script.php", Method.POST);
request.AddParameter("application/json; charset=utf-8", "json string", ParameterType.RequestBody);
request.RequestFormat = DataFormat.Json;
client.ExecuteAsync(request, response =>
{
if (response.StatusCode == HttpStatusCode.OK)
{
//OK
}
});
}

My Windows Phone app Get empty response (404 Not Found) Scond time, work's great first time;And always work fine if without SSL

I am building my first windowsPhone 8.1 application ,the role of my application is to create connection with server to get information from it, so I am writing the code to do this process by sending json-rpc request to server to get some information ,I am successful to get it in first time but when I send the second request I am receiving an empty response with 404 error (page not found).
But when I call the service without https (http only) it works fine regardless how many time I call it !
public async Task<string> GetDataFromServer(string urlToCall, string JSONData,string RR)
{
string UserName = “XXXXXXX”
string Password = "XXX";
using ( var handler = new HttpClientHandler())
{
handler.Credentials = new NetworkCredential(UserName, Password);
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = null;
try
{
response = await client.PostAsync(urlToCall, new StringContent(JSONData.ToString(), Encoding.UTF8, " application/json"));
string res = response.Content.ReadAsStringAsync().Result;
Windows.UI.Popups.MessageDialog g = new Windows.UI.Popups.MessageDialog(res);
await g.ShowAsync();
return res;
}
catch (Exception ex)
{
Windows.UI.Popups.MessageDialog g = new Windows.UI.Popups.MessageDialog("Error is : " + ex.Message);
g.ShowAsync();
return "Error";
}
finally
{
response.Dispose();
client.CancelPendingRequests();
client.Dispose();
handler.Dispose();
}
}
}
Again, when call the URL of service (start with https) on first time I got response with seeked data, but second time I receive an empty response with 404 error (page not found) !!
Any help please
Please try to use this solution.
public async Task<string> SendJSONData3(string urlToCall, string JSONData)
{
string UserName = "XXXXXXXXX";
string Password = "XXXXXXXXX";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlToCall);
httpWebRequest.Credentials = new NetworkCredential(UserName, Password);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync()))
{
string json = JSONData;
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return result;
}
}
A couple of ideas:
Do not use the .Result property. Just use await instead to avoid deadlocks.
Remove the additional space in front of the media type parameter " application/json"
Enable logging on the webserver and see if the second request arrives on the server.
Get a network trace, for example with Wireshark or Fiddler.
Try puting WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); in your initialization code, as proposed in this answer.

No ServiceStack WebServiceException.ResponseStatus on csv format request

When unit testing, I want to check csv formatted results, so I have the following code in my test.
MyDtoReq request = new MyDtoReq();
// ... assign some properties
string url = request.ToUrl("GET");
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
httpReq.Accept = "text/csv";
csv = new StreamReader(httpReq.GetResponse().GetResponseStream()).ReadToEnd();
That works fine, if the request succeeds. But when it fails, it raises a System.Net.WebException that doesn't have the expected WebServiceException.ResponseStatus details. NUnit reports the exception as follows:
Test Name: TestReq
Test FullName: [...].TestReq
Test Source: c:\Users\[...]\UnitTestProject1\ServiceTests.cs : line 261
Test Outcome: Failed
Test Duration: 0:00:27.104
Result Message: System.Net.WebException : The remote server returned an error: (400) Bad Request.
Result StackTrace: at [...].TestReq() in c:\Users\[...]\UnitTestProject1\ServiceTests.cs:line 287
Turns out that this is by design, as most clients requesting csv format are not able to parse a ResponseStatus. In order to see the actual error, I would re-submit the request with format=html in the browser - a frustrating waste of time.
Here's how to get the actual error message from failing csv format requests:
// Declared in test setup
public const string Host = "http://localhost:1337";
private const string BaseUri = Host + "/";
[Test]
public void TestMyDtoReqCsvFormat()
{
MyDtoReq request = new MyDtoReq();
request.startDate = "20130919";
request.endDate = "20130930";
request.source = "Token";
try
{
string requestUrl = request.ToUrl("GET");
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(requestUrl);
httpReq.Accept = "text/csv";
var csv = new StreamReader(httpReq.GetResponse().GetResponseStream()).ReadToEnd();
// assert some facts about the contents of csv
}
catch (Exception)
{
try {
JsonServiceClient client = new JsonServiceClient(BaseUri);
MyDtoReqResponse response = client.Get(request);
// do something if re-request succeeds (i.e. was a transient error)
}
catch (WebServiceException webEx)
{
var message = webEx.ResponseStatus.ErrorCode +
" " + webEx.ResponseStatus.Message.Trim() +
" " + webEx.ResponseStatus.StackTrace.Trim();
throw new WebException(message,webEx);
}
catch (Exception otherEx) {
System.Diagnostics.Debug.WriteLine(otherEx.Message);
throw new Exception(otherEx.Message, otherEx);
}
}
}