Xamarin Android POSTing to WebAPI - c#

I have a WebAPI service running on a server, and I am able to hit against it all day long in an MVC app I have. I am now trying to create an Xamarin Android app that also hits against the same WebAPI. I put together some code in a console app to test, and it works just fine. However, when I put the same code in my Xamarin Android app, it cannot connect to the service, I get back an aggregate exception that basically wraps a WebException. Digging into the exception further, it seems it is a System.Net.WebExceptionStatus.ConnectFailure type of error.
Here is the code:
using (HttpClient webAPI = new HttpClient())
{
// hardcode the request to try and see why it errors
AuthUserRequest thisUser = new AuthUserRequest
{
UserName = "username",
Password = "password",
AppName = "Dashboard"
};
webAPI.MaxResponseContentBufferSize = 256000;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(thisUser);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response;
try
{
response = await webAPI.PostAsync("It'sOurURL", content);
}
catch (Exception err)
{
string sHold = err.Message;
throw;
}
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
Context thisContext = Application.Context;
Toast toast = Toast.MakeText(thisContext, "Successful", ToastLength.Short);
toast.Show();
}
}
As I said it's weird it works just fine from a Console app, just not the Xamarin Android app. Any insight at all into this?

All looks pretty good. My API calls are working in Xamarin Android and iOS. My code is pretty much the same with two real minor differences. I have set ConfigureAwait(false) on the PostAsync call. Additionally I have created a URI variable with the address for the API endpoint and passed that into the PostAsync method, rather then using a hard coded string.
using (var client = new HttpClient())
{
var user = new CredentialsModel
{
Password = password,
Username = username,
};
var uri = new Uri("YOUR_URL_GOES_HERE");
var json = JsonConvert.SerializeObject(user);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync(uri, content).ConfigureAwait(false);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
var authData = JsonConvert.DeserializeObject<ResponseModel>(responseContent);
return authData;
}
return null;
}

It was my own bone-headed mistake... When I tried the URL this morning, it was there, but the IT department has been mucking about with the server, so it's no longer available externally. Sorry to bother everyone with this.

Related

POST from Form to external API

I'm quite noob into asp.net
I'm building a simple solution in VS2019, using asp.net MVC, which is supposed to send a User data to another API which will be responsible for saving into database.
So far, both APIs are REST and I'm not using core
Basically, it is a form with a submit that will POST to the external project, pretty simple.
I'm following some tutorials and stuff but there are so many different ways that got me confused, so I decided to ask here
Here's what I have so far
[HttpPost]
public async ActionResult Index(User user)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://pathtoAPI.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
User newuser = new User()
{
email = user.email,
nome = user.nome,
cpf = user.cpf,
cnpj = user.cnpj,
userName = user.userName,
senha = user.senha,
telefone = user.telefone
};
var jsonContent = JsonConvert.SerializeObject(newuser);
var contentString = new StringContent(jsonContent, Encoding.UTF8, "application/json");
contentString.Headers.ContentType = new
MediaTypeHeaderValue("application/json");
//contentString.Headers.Add("Session-Token", session_token);
HttpResponseMessage response = await client.PostAsync("register", contentString);
return Content($"{response}");
}
I want to receive the "OK" message from the other API and just print it on my screen, I'm using the cshtml file to handle the front and the form as well.
The return though seems to be wrong, it's expecting either 'null, task, task, or something like.
Can someone please help me with this code?
Thanks
You need to return the content of the response, not the response object itself.
HttpResponseMessage response = await client.PostAsync("register", contentString);
string responseBody = await response.Content.ReadAsStringAsync();
return Content(responseBody);

PostAsync() method is not working in dot-net core window service

I am trying the run the console application as a window service in dot-net core and able to create ,start ,stop and delete the service.
I am using PostAsync() method in application but the issue is that this method is working perfectly in console application but in window service most of the times PostAsync() never returned any response.
Any help would be highly appreciated. Thanks!
string abc=\"firstparameter\":\"English\",\"secondParameter\":\"Hindi\",\"Marks\":\"Result \"}";
var response = await client.PostAsync("url", new StringContent(ab, Encoding.UTF8, "application/json")).ConfigureAwait(false))
and By this way
var response = client.PostAsync("url", content).Result;
var objResult = response.Content.ReadAsStringAsync().Result;
I know this is a late response but just in case this could help someone here is my answer.
StringContent is not a good option when doing Post. You may want to use FormUrlEncodedContent:
var data = new Dictionary<string, string>
{
{ "firstparameter", "firstparametervalue" },
{ "secondparameter", "secondparametervalue" }
};
var content = new FormUrlEncodedContent(data);
var response = await client.PostAsync(apiUri, content);
Here is an example of how you can use it.
In this example I have used HttpClientFactory to create a HttpClient.
var userApi = httpClientFactory.CreateClient("UserApi");
var request = new HttpRequestMessage(HttpMethod.Post, "systemuser/login");
request.Content = new StringContent(JsonConvert.SerializeObject(loginRequest), Encoding.UTF8, "application/json");
var response = await userApi.SendAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
var body = await response.Content.ReadAsAsync<LoginResponse>();
return Ok(body);
}
return StatusCode((int)response.StatusCode);
Hope this helps

Post request to api works for Android but not for IOS. Xamarin

I've been trying to create a function that validates my token and it works for Android but not for IOS. The response on IOS from my custom API returns 401 unauthorized (which it should when you send an invalid key), but I've tried the key in postman and it's valid.
Maybe this has something to do with formatting? But I don't see what would be the difference between Android and IOS.
Some code:
public async Task<T> PostResponse<T>(string weburl, string jsonstring) where T : class
{
var Token = App.TokenDatabase.GetToken();
string ContentType = "application/json";
var token = string.Format("Token token={0}", Token.access_token);
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
try
{
var result = await client.PostAsync(weburl, new StringContent(jsonstring, Encoding.UTF8, ContentType));
if(result.StatusCode == System.Net.HttpStatusCode.OK)
{
var JsonResult = result.Content.ReadAsStringAsync().Result;
try
{
var ContentResp = JsonConvert.DeserializeObject<T>(JsonResult);
return ContentResp;
}
catch { return null; }
}
}
catch { return null; }
return null;
}
I've created some logs on the server side and when I run the IOS app the token doesn't get passed on to the server for some reason.
PHP code:
$token = null;
$headers = apache_request_headers();
if(isset($headers['Authorization'])){
$matches = array();
preg_match('/Token token=(.*)/', $headers['Authorization'], $matches);
if(isset($matches[1])){
$token = $matches[1];
}
}
file_put_contents('../config/log.txt', $token);
I've checked that the app sends the token. It disappears somewhere on the way.
EDIT: After some logging, I found out that the token is in the header but not in the Authorization header when sent through IOS, how do you solve this?
I solved it by downloading Flurl and Flurl.Http plugins and added this line:
var result = await url.WithOAuthBearerToken(token).PostJsonAsync(jsonstring);
Credit: Xamarin forms not sending Authorization header
Also
Fluent HTTP

Quickbook invoice create returns 400 bad request (SANDBOX, Quickbook Online, C# MVC, OAuth 2.0)

Well, I am working on creating Quickbook online entries, and for some reason I am getting error (400 bad request).
I know something is invalid in my request but I am unable to figure it out. I am using Sandbox account. I have copied default data from API explorer and made request using this data only and finally getting 400 bad request.
My code is working fine for "Select * from invoice" query request.
The base URL I am using sandbox-quickbooks.api.intuit.com
My Code is as Follow:-
var principal = User as ClaimsPrincipal;
Session["realmId"] = XXXXXXX;
var result = new HttpResponseMessage();
if (Session["realmId"] != null)
{
string realmId = Session["realmId"].ToString();
string qboBaseUrl = ConfigurationManager.AppSettings["QBOBaseUrl"];
//add qbobase url and query
string uri = string.Format("{0}/v3/company/{1}/invoice", qboBaseUrl, realmId);
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + Session["AccessToken"]);
result = await client.PostAsync(uri, new StringContent(JsonData, System.Text.Encoding.UTF8, "application/json"));
return result;
}
catch (Exception ex)
{
return result;
}
}
else
return result;
Check the body of the response and see if it provides any details about what is wrong with the request.
//...
var responseContent = await result.Content.ReadAsStringAsync();
//...

Parsing from a PHP page from PCL with Xamarin Forms

as beginner of Xamarin PCL i have the next question:
I build a project in VS2017. I want to scan a barcode and then send the information to a PHP website and see if the products exists.
The scanning part works great. But now i want to send the information from the PCL to the PHP website like this:
string barcode_check_uri = "https://mywebsite.com/websrv/barcode_checker.php";
string _barcode = result.Text;
HttpClient client = new HttpClient();
var uri = new Uri(barcode_check_uri);
ProductBarcode product = new ProductBarcode();
product.Barcode = _barcode;
string json = Newtonsoft.Json.JsonConvert.SerializeObject(product);
try
{
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync(uri, content).Result;
if (response.Content != null && response.IsSuccessStatusCode)
{
//Error here
string _respnstring = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
}
catch (HttpRequestException e)
{
System.Diagnostics.Debug.WriteLine(e);
}
I got a error at the 'string _respnstring = await response.Content.ReadAsStringAsync().ConfigureAwait(false);'. I dont know how it looks like on the PHP side and on the PCL side.
Can anyone help please. Many thanx ...

Categories