Ich habe Probleme, POST HTTP-Client mit Accept, ContentType und Authentifizierung als Header verwenden. Ich versuchte mehrere Implementierungen mit send aysnc und post async und alle schienen zu versagen. Ich habe https://www.hurl.it/ verwendet, um sicher zu gehen, dass es auch einen Timeout-Fehler gab, aber es funktioniert, und JSON-Daten zurückbekommen.Timeout-Fehler: HttpClient POST
Ich bekomme eine Verbindung Timeout Fehler Ausnahme (in: var response = await client.SendAsync(request);
) bei der Ausführung der unten genannten Code.
Auch ich habe curl-Befehl, den ich versuchte, von zu gehen, die unten auch aufgeführt ist.
Umsetzung:
// Body
var dictionary = new Dictionary<string, string>();
dictionary.Add("id", "123");
var formData = new List<KeyValuePair<string, string>>();
formData.Add(new KeyValuePair<string, string>("id", "123"));
// Http Client
var client = new HttpClient();
client.BaseAddress = new Uri("https://www.some_website.com/api/house");
// Http Request
var request = new HttpRequestMessage(HttpMethod.Post, "https://www.some_website.com/api/house");
// Accept
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// Content-Type
//request.Content = new FormUrlEncodedContent(dictionary);
var httpContent = new StringContent(
JsonConvert.SerializeObject(
dictionary,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}),
Encoding.UTF8,
"application/json");
request.Content = new StringContent(
JsonConvert.SerializeObject(
dictionary,
new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
}),
Encoding.UTF8,
"application/json");
// Authentication
var byteArray = new UTF8Encoding().GetBytes("user" + ":" + "pass");
//client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var header = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
client.DefaultRequestHeaders.Authorization = header;
// Communication
try
{
var response = await client.SendAsync(request);//PostAsync("https://www.some_website.com/api/house", httpContent);
if (response.IsSuccessStatusCode)
{
var myContent = await response.Content.ReadAsStringAsync();
var deliveryStatus = JsonConvert.DeserializeObject<MyOjbect>(myContent);
}
}
catch (Exception e)
{
//catch error
}
Curl:.
curl -i --user user:123 -H Accept:application/json -H content-type:application/json -H cache-control:no-cache -X POST https://www.some_website.com/api/house -H Content-Type: application/json -d '{"id": "123"}'
Sie legen Ihre BaseAddress und die HttpRequestMessage Uri-Eigenschaft beide auf die gleiche absolute URL, sollte es nicht die Basisadresse sein (zB https://somewebsite.com) und die relative URL (ex. (api/Haus)? Sie können auch Ihre Anfrage mit Fiddler http://www.telerik.com/fiddler überprüfen –
Ich denke, dass Sie richtig sind. Ich machte die Änderungen und immer noch eine Verbindung Timeout Fehler – PLOW
Das Problem wurde gelöst! Der Code ist korrekt. Das Problem war, dass ich die Schule Campus Wifi war so, dass ich hinter einem Proxy war. Nach Hause gehen und meine Wifi gibt mir keine Auszeit Fehler – PLOW