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
}
});
}
Related
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
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.
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.
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.
I would like to send POST request in windows phone 8 environment my code is running successfully but i am getting NotFound exception. Its mean is i want to POST some data but i am sending null. So please let me know how to send POST Request asynchronously with Data in windows phone 8 environmet. I tried following links but not helpful.
link link2
I approached like this
private async Task<LastRead> SyncLastReadPOST(LastRead lastreads, bool actionStatus)
{
string jsondata = "";
actionStatus = false;
apiData = new LastReadAPI()//It is global variable from apiData this object has the information
{
AccessToken = thisApp.currentUser.AccessToken,
Book = lastreads.Book,
Page = lastreads.Page,
Device = lastreads.Device
};
jsondata = Newtonsoft.Json.JsonConvert.SerializeObject(apiData);
LastRead responsedData = new LastRead();
Uri lastread_url = new Uri(string.Format("{0}lastread", url_rootPath));
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(lastread_url);
webRequest.ContentType = "application/json";
webRequest.Accept = "application/json;odata=verbose";
webRequest.Method = "POST";
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
}
catch { }
return responsedData;
}
private void GetRequestStreamCallback(IAsyncResult ar)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
Stream postStream = request.EndGetRequestStream(ar);
var input = Newtonsoft.Json.JsonConvert.SerializeObject(jsondata);//jsondata is my global data variable in json format.
byte[] byteArray = Encoding.UTF8.GetBytes(input);
postStream.WriteAsync(byteArray, 0, byteArray.Length);
postStream.Close();
request.BeginGetResponse(new AsyncCallback(GetResponseStreamCallback), request);
}
private void GetResponseStreamCallback(IAsyncResult ar)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)ar.AsyncState;
HttpWebResponse response;
//In following line i am getting the exception notFound.
response = (HttpWebResponse)webRequest.EndGetResponse(ar);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReaders = new StreamReader(streamResponse);
var responces = streamReaders.ReadToEnd();
streamResponse.Close();
streamReaders.Close();
response.Close();
}
catch(Exception ex)
{
}
}
As far as i know notFound exceptions comes when we are not posting any data while using the POST request method. you can see i have mentioned the data i am passing into the GEtRequestStreamCallback. I have mentioned a note. Please help me. Where i am going to wrong.
Try setting the content-type to application/json; charset=utf-8
Also, you can do all that stuff in nicer and shorter way(sample):
var wc = new WebClient();
//SET AUTH HEADER IF NECESSARY
//wc.Headers["Authorization"] = "OAUTH "+TOKEN;
wc.Headers["Content-Type"] = "application/json;charset=utf-8";
wc.UploadStringCompleted += (s, er) =>
{
if (er.Error != null) MessageBox.Show("Error\n" + er.Error);
else MessageBox.Show(er.Result);
};
string data = JsonConvert.SerializeObject(MY_DATA_OBJECT);
MessageBox.Show(data);
wc.UploadStringAsync(new Uri(POST_URI), "POST", data);
I did it with the help of HttpClient inplace of WebClient. Following few lines will do magic. :)
HttpClient hc = new HttpClient();
hc.BaseAddress = new Uri(annotation_url.ToString());
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, myUrl);
HttpContent myContent = req.Content = new StringContent(myJsonString, Encoding.UTF8, "application/json");
var response = await hc.PostAsync(myUrl, myContent);
//Line for pull out the value of content key value which has the actual resposne.
string resutlContetnt = response.Content.ReadAsStringAsync().Result;
DataContractJsonSerializer deserializer_Json = new DataContractJsonSerializer(typeof(MyWrapperClass));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(resutlContetnt.ToString()));
AnnotateResponse = deserializer_Json.ReadObject(ms) as MyWrapperClass;