How to call the async Task Function? - asp.net-web-api

My code for class AllWebApiOperations is
public AllWebApiOperations(string apiURI)
{
client = new HttpClient();
client.BaseAddress = new Uri(apiURI);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
public async Task<string> GetDataAsync(string route)
{
string result= string.Empty;
HttpResponseMessage response = await client.GetAsync(route);
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsAsync<string>();
}
return result;
}
I'm calling this in button click
string apiURI = #"http://localhost:35487/";
private async void btnCallWebApi_Click(object sender, EventArgs e)
{
AllWebApiOperations op = new AllWebApiOperations(apiURI);
var result = await op.GetDataAsync(apiURI + "api/products/");
Console.WriteLine(result);
}
My code web api is working properly as shown below
But I'm getting error while calling the function as shown below
I'm not sure why I'm getting this error, tried googling but can't find resolution.

Found the answer:
public async Task<string> GetDataAsync(string route)
{
string result= string.Empty;
HttpResponseMessage response = await client.GetAsync(route);
if (response.IsSuccessStatusCode)
{
**result = await response.Content.ReadAsAsync<string>();**
}
return result;
}
I should be using
**result = await response.Content.ReadAsStringAsync();**

Related

post request not working in xamarin forms

im trying to send a HTTP POST request from xamarin forms app to a webservice API on ASP.NET MVC, However, when i do it, i get status code 500 back from the server. I suspect it has something to do with the values in sending to API but i have not been able to figure it out.
Anyhelp is much appeciated
here is the
WEB API CODE
public void LogInFromMobile([FromBody] List<Creds> detail)
{
......
}
here is the
XAMARIN-FORMS APP CODE
public async void Button_Clicked(object sender, EventArgs e)
{
try
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Creds cr = new Creds();
cr.username = user.Text;
cr.password = pass.Text;
List<Creds> cred = new List<Creds>();
cred.Add(cr);
string url = #"http://IPHERE/LoginWebService.asmx/LogInFromMobile";
string json = JsonConvert.SerializeObject(cred);
HttpContent content = new StringContent(json);
var response = await client.PostAsync(url, content);
if (response.IsSuccessStatusCode)
{
await DisplayAlert("Alert", "Success", "Ok");
return;
}
else
{
await DisplayAlert("Alert", "Error", "Ok");
return;
}
}
}
catch(Exception ex)
{
await DisplayAlert("", ex.Message, "ok");
}
}

download in async httpclient xml file

I want to download an xml file in asynchronous and let the user know that the file is downloading while it does other moves in the app
public static async Task<string> GetRequestAsync(string url)
{
using (var httpClient = new HttpClient() { MaxResponseContentBufferSize = int.MaxValue })
{
HttpResponseMessage response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
with this command did not work successfully

Return an HttpResponse in an APIController Method which calls a different API

I have an APIController Method as below. Basically I need to validate an API response. So it's an API call within an API call.
public class DCController : ApiController
{
[HttpPost]
public HttpResponseMessage SampleMethod(string url)
{
var uri = new Uri(url);
var baseAddress = uri.GetLeftPart(System.UriPartial.Authority);
var apiAddress = url.Replace(baseAddress + "/", "");
var responseString = string.Empty;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = client.GetAsync(apiAddress).Result;
if (response.IsSuccessStatusCode)
{
responseString = response.Content.ReadAsStringAsync().Result;
}
}
if (!string.IsNullOrEmpty(responseString) && responseString.ToString().Validate())
{
return Request.CreateResponse(HttpStatusCode.OK, "Validated");
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid");
}
}
Issue:
1. Request object is null in the return lines.
2. If i try creating a request object -
var request = new HttpRequestMessage();
It throws below error:
An exception of type 'System.InvalidOperationException' occurred in
System.Web.Http.dll but was not handled in user code
Additional information: The request does not have an associated
configuration object or the provided configuration was null.
Not sure what settings I need to add. I am relatively new to working with APIs. Any help is appreciated.
I could get it working by below code -
[HttpPost]
public HttpResponseMessage Get(string url)
{
string responseString = GetWebApiData(url); //Extracted the method
HttpResponseMessage response = new HttpResponseMessage();
if (!string.IsNullOrEmpty(responseString) && responseString.ToString().Validate())
{
response.ReasonPhrase = "Valid";
response.StatusCode = HttpStatusCode.OK;
}
else
{
response.ReasonPhrase = "Invalid";
response.StatusCode = HttpStatusCode.BadRequest;
}
return response;
}

How to send pre request before every request in windows phone 7

I want to send one pre request to my server before send every request. From that pre request I will receive the token from my server and than I have to add that token into all the request. This is the process.
I have try with some methods to achieve this. But I am facing one problem. That is, When I try to send pre request, it is processing with the current request. That mean both request going parallel.
I want to send pre request first and parse the response. After parsing the pre request response only I want to send that another request. But first request not waiting for the pre request response. Please let me any way to send pre request before all the request.
This is my code:
ViewModel:
`public class ListExampleViewModel
{
SecurityToken sToken = null;
public ListExampleViewModel()
{
GlobalConstants.isGetToken = true;
var listResults = REQ_RESP.postAndGetResponse((new ListService().GetList("xx","xxx")));
listResults.Subscribe(x =>
{
Console.WriteLine("\n\n..................................2");
Console.WriteLine("Received Response==>" + x);
});
}
}`
Constant Class for Request and Response:
`public class REQ_RESP
{
private static string receivedAction = "";
private static string receivedPostDate = "";
public static IObservable<string> postAndGetResponse(String postData)
{
if (GlobalConstants.isGetToken)
{
//Pre Request for every reusest
receivedPostDate = postData;
GlobalConstants.isGetToken = false;
getServerTokenMethod();
postData = receivedPostDate;
}
HttpWebRequest serviceRequest =
(HttpWebRequest)WebRequest.Create(new Uri(Constants.SERVICE_URI));
var fetchRequestStream =
Observable.FromAsyncPattern<Stream>(serviceRequest.BeginGetRequestStream,
serviceRequest.EndGetRequestStream);
var fetchResponse =
Observable.FromAsyncPattern<WebResponse>(serviceRequest.BeginGetResponse,
serviceRequest.EndGetResponse);
Func<Stream, IObservable<HttpWebResponse>> postDataAndFetchResponse = st =>
{
using (var writer = new StreamWriter(st) as StreamWriter)
{
writer.Write(postData);
writer.Close();
}
return fetchResponse().Select(rp => (HttpWebResponse)rp);
};
Func<HttpWebResponse, IObservable<string>> fetchResult = rp =>
{
if (rp.StatusCode == HttpStatusCode.OK)
{
using (var reader = new StreamReader(rp.GetResponseStream()))
{
string result = reader.ReadToEnd();
reader.Close();
rp.GetResponseStream().Close();
XDocument xdoc = XDocument.Parse(result);
Console.WriteLine(xdoc);
return Observable.Return<string>(result);
}
}
else
{
var msg = "HttpStatusCode == " + rp.StatusCode.ToString();
var ex = new System.Net.WebException(msg,
WebExceptionStatus.ReceiveFailure);
return Observable.Throw<string>(ex);
}
};
return
from st in fetchRequestStream()
from rp in postDataAndFetchResponse(st)
from str in fetchResult(rp)
select str;
}
public static void getServerTokenMethod()
{
SecurityToken token = new SecurityToken();
var getTokenResults = REQ_RESP.postAndGetResponse((new ServerToken().GetServerToken()));
getTokenResults.Subscribe(x =>
{
ServerToken serverToken = new ServerToken();
ServiceModel sm = new ServiceModel();
//Parsing Response
serverToken = extract(x, sm);
if (!(string.IsNullOrEmpty(sm.NetErrorCode)))
{
MessageBox.Show("Show Error Message");
}
else
{
Console.WriteLine("\n\n..................................1");
Console.WriteLine("\n\nserverToken.token==>" + serverToken.token);
Console.WriteLine("\n\nserverToken.pk==>" + serverToken.pk);
}
},
ex =>
{
MessageBox.Show("Exception = " + ex.Message);
},
() =>
{
Console.WriteLine("End of Process.. Releaseing all Resources used.");
});
}
}`
Here's couple options:
You could replace the Reactive Extensions model with a simpler async/await -model for the web requests using HttpClient (you also need Microsoft.Bcl.Async in WP7). With HttpClient your code would end up looking like this:
Request and Response:
public static async Task<string> postAndGetResponse(String postData)
{
if (GlobalConstants.isGetToken)
{
//Pre Request for every reusest
await getServerTokenMethod();
}
var client = new HttpClient();
var postMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(Constants.SERVICE_URI));
var postResult = await client.SendAsync(postMessage);
var stringResult = await postResult.Content.ReadAsStringAsync();
return stringResult;
}
Viewmodel:
public class ListExampleViewModel
{
SecurityToken sToken = null;
public ListExampleViewModel()
{
GetData();
}
public async void GetData()
{
GlobalConstants.isGetToken = true;
var listResults = await REQ_RESP.postAndGetResponse("postData");
}
}
Another option is, if you want to continue using Reactive Extensions, to look at RX's Concat-method. With it you could chain the token request and the actual web request: https://stackoverflow.com/a/6754558/66988

Modify Request.Content in WebApi DelegatingHandler

I need to modify requested content to replace some characters (because of some unicode problems). Previously (in ASP.NET MVC), I did this with HttpModules; but in WebApi, it seems that I should DelegatingHandler but it is totally different.
How can I modify request.Content inside the SendAsync method? I need something like this:
protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
var s = await request.Content.ReadAsStringAsync();
// do some modification on "s"
s= s.replace("x","y");
request.Content = new StringContent(s);
var response = await base.SendAsync(request, cancellationToken);
return response;
}
In the code above, I think I should check the request's content type and then decide what to do. If yes, which checks should I do?
I did something like this in SendAsync. Although it is not a comprehensive solution, it works:
//first : correct the URI (querysting data) first
request.RequestUri = new Uri(Correcr(request.RequestUri.ToString()));
var contentType = request.Content.Headers.ContentType;
if (contentType != null)
{
if (contentType.MediaType == "application/x-www-form-urlencoded")//post,put,... & other non-json requests
{
var formData = await request.Content.ReadAsFormDataAsync();
request.Content = new FormUrlEncodedContent(Correct(formData));
}
else if (contentType.MediaType == "multipart/form-data")//file upload , so ignre it
{
var formData = await request.Content.ReadAsFormDataAsync();
request.Content = new FormUrlEncodedContent(Correct(formData));
}
else if (contentType.MediaType == "application/json")//json request
{
var oldHeaders = request.Content.Headers;
var formData = await request.Content.ReadAsStringAsync();
request.Content = new StringContent(Correct(formData));
ReplaceHeaders(request.Content.Headers, oldHeaders);
}
else
throw new Exception("Implement It!");
}
return await base.SendAsync(request, cancellationToken);
and this helper function:
private void ReplaceHeaders(HttpContentHeaders currentHeaders, HttpContentHeaders oldHeaders)
{
currentHeaders.Clear();
foreach (var item in oldHeaders)
currentHeaders.Add(item.Key, item.Value);
}

Resources