POST with Restsharp doesn't work as expected - restsharp

I have the following code:
var Client = new System.Net.WebClient();
var Response = Client.UploadString("myurl", PostData);
it works as expected
I am trying to do the same with Restsharp:
var Client = new RestClient("myurl");
var Request = new RestRequest(Method.POST);
Request.AddBody(PostData);
var Response = client.Execute(request);
doesn't work, and:
var Client = new RestClient("myurl");
var Request = new RestRequest(Method.POST);
Request.AddParameter("application/x-www-form-urlencoded; charset=UTF-8", PostData);
var Response = client.Execute(request);
doesn't work either...
what am I missing?

Related

HttpClientFactory increased response time with high load

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.

xamarin.forms ,Foursqaure api response is not displaying but works in postman and foursqaure site after providing api key ?where im wrong

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?

Authentication in AngleSharp

I need to parse a website but this one has Windows authentication (kerberos), I authenticated with HttpRequester but How can I pass the authentication to AngleSharp?
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Credentials = new NetworkCredential("user", "pass");
req.CookieContainer = session;
After that, I don't know how to continue to pass the session to AngleSharp connection:
var configuration = Configuration.Default.WithDefaultLoader().WithCookies();
var context = BrowsingContext.New(configuration);
await context.OpenAsync(WebsiteUrl);
You need to NuGet the AngleSharp.Io package, that will enable the "WithRequesters"
And then:
var credentials = new NetworkCredential("user", "pass", "domain");
var handler = new HttpClientHandler { Credentials = credentials };
var config = Configuration.Default.WithRequesters(handler).WithCookies();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync(url)

How to get Location in Response Header use HttpResponseMessage in windows phone

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();

ReasonPhrase: 'Forbidden' HttpResponseMessage winrt

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?