i'm getting this error :ReasonPhrase: 'Forbidden' in the response variable:
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(uri);
string result = await response.Content.ReadAsStringAsync();
besides i used it with an other url and it works fine ! any ideas please?
Related
I am connecting to SOAP web service and I am using HttpClientFactory. Application is running on docker. Tester were testing load test with jmeter. After putting more and more load on application response time was increasing forom 100 ms to 50 sec. They have tried Soap service directly and it is performing ok.
var _client = _httpClientFactory.CreateClient("Servis");
var request = new HttpRequestMessage(
HttpMethod.Post,
"")
{
Content = new StringContent(soapRequest, Encoding.UTF8, "text/xml")
};
_client.DefaultRequestHeaders.ConnectionClose = true;
StringContent httpContent = new StringContent(soapRequest, Encoding.UTF8, "text/xml");
using var response = await _client.PostAsync(_client.BaseAddress, httpContent);
var stream = await response.Content.ReadAsStreamAsync();
var content = await _streamHelper.StreamToStringAsync(stream);
if (response.IsSuccessStatusCode)
return content;
throw new ApiException
{
StatusCode = (int)response.StatusCode,
Content = content
};
But I have tried RESTSharp and I have same problem
System.Net.ServicePointManager.DefaultConnectionLimit = 10;
var postUrl = _configuration["OibSettings:BaseAddress"];
IRestClient client = new RestClient();
client.ConfigureWebRequest((r) =>
{
r.ServicePoint.Expect100Continue = false;
r.KeepAlive = false;
});
IRestRequest request = new RestRequest
{
Resource = postUrl,
Method = Method.POST
};
request.AddHeader("Content-Type", "text/xml");
request.AddHeader("Accept", "text/xml");
request.AddParameter("text/xml", soapRequest, ParameterType.RequestBody);
var response = await client.ExecuteAsync(request);
if (response.StatusCode == HttpStatusCode.OK)
return response.Content;
throw new ApiException
{
StatusCode = (int)response.StatusCode,
Content = response.Content
};
It starts working OK but after some time response time start to increse and. Around 5% of responses have extreme time response compared to targeted 100 ms on response.
Also some requests are timeouting and end up in error. But directly jmiter on that service it is ok.
private async Task GetresultAsync()
{
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri("https://api.foursquare.com/v3/places/search?ll=15.3494005,75.142583&query=shops&fields=geocodes,categories,name,hours,fsq_id,price,rating,stats,location"),
Headers =
{
{ "Accept", "application/json" },
{ "Authorization", "fsq322aNlTt3+PuRKw5js/ndngtry/XxNV0Q70yzKjDTQn0="
},
},
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Debug.WriteLine(body);
}
Please any suggestions? I'm very new to xamarin and I'm getting data in postman but not getting result in xamarin using
RESTCLIENT OR HTTPCLIENT for( foursqaure places api for v3) where am I wrong?
I am new to flutter and I am using mongodb to save the credentials from signup page. When tried to give credentials that already exists server shows a response - 'user already exits' this response was viewed in postman. I am able to get statusCode but I am unable to get the same response in flutter. below is my flutter code.
Future<String> uploadImage(filename) async {
var request = http.MultipartRequest('POST', Uri.parse(serverReceiverPath));
request.files.add(await http.MultipartFile.fromPath('file', filename));
var res = await request.send();
print(res.statusCode);
return null;
}
To get the body response, use res.stream.bytesToString()
Complete code:
Future<String> uploadImage(filename) async {
var request = http.MultipartRequest('POST', Uri.parse(serverReceiverPath));
request.files.add(await http.MultipartFile.fromPath('file', filename));
var res = await request.send();
print(res.statusCode); // status code
var bodyResponse = await res.stream.bytesToString(); // response body
print(bodyResponse);
return null;
}
I need your help guys. I'm developing a front-end with Blazor which sends request to ASP.Net Core.
I have the following code which gets an API response, in this case it returns the entire body of the response. What I'm trying to get here is the status code of the response only, example (200).
await Http.SendJsonAsync(HttpMethod.Post, "https://da3.mock.pstmn.io/api/register", CurrentUser);
var response = await Http.GetStringAsync("/api/register");
Console.WriteLine(response);
Use the other GetAsync method.
//var response = await Http.GetStringAsync("/api/register");
//Console.WriteLine(response);
var response = await Http.GetAsync("/api/register");
Console.WriteLine(response.StatusCode); // also see response.IsSuccessStatusCode
For POST method you could use SendAsync, you need to use PMC to install Newtonsoft.Json package firstly:
var requestMessage = new HttpRequestMessage()
{
Method = new HttpMethod("POST"),
RequestUri = new Uri("https://localhost:5001/api/default"),
Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(CurrentUser))
};
requestMessage.Content.Headers.ContentType =
new System.Net.Http.Headers.MediaTypeHeaderValue(
"application/json");
var result = await Http.SendAsync(requestMessage);
var responseStatusCode = result.StatusCode;
For GET method,what Henk Holterman has suggested (use Http.GetAsync) works well.
Refer to
https://learn.microsoft.com/en-us/aspnet/core/blazor/call-web-api?view=aspnetcore-3.0#httpclient-and-httprequestmessage-with-fetch-api-request-options
I want get Location in Response Header but i can't see this in Response. How to get this. Please help me.
var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false });
FormUrlEncodedContent postData = new FormUrlEncodedContent(new[] {
new KeyValuePair<string,string>("background","1"),
new KeyValuePair<string,string>("line1","Content1"),
new KeyValuePair<string,string>("line2","Content2")
});
HttpResponseMessage response = await client.PostAsync("URL", postData);
string rp = await response.Content.ReadAsStringAsync();