通过 HttpClient 向 Saucelabs 发送 PUT 请求没有响应
Sending PUT request via HttpClient to Saucelabs not responding
我正在尝试发送 PUT
请求以更新 Saucelabs via their API 上的职位。但是,以下代码挂起,我不确定为什么。
using (var client = new HttpClient())
{
var sessionId = Browser.Driver.GetSessionId();
var uri = new Uri($"https://saucelabs.com/rest/v1/{Configuration.SauceUserName}/jobs/{sessionId}");
var uriWithCred =
new UriBuilder(uri)
{
UserName = $"{Configuration.SauceUserName}",
Password = $"{Configuration.SauceAccessKey}"
}.Uri;
var payload = new StringContent($"{{\"name\":\"{TestMethodName}\"}}", Encoding.UTF8, "application/json");
var request = new HttpRequestMessage
{
Method = HttpMethod.Put,
RequestUri = uriWithCred,
Content = payload
};
var response = client.SendAsync(request).Result;
}
以下 cUrl 请求成功(当然是经过编辑的凭据)。
curl -X PUT -s -u <username>:<access-key>
-d "{\"name\": \"test name\"}"
https://saucelabs.com/rest/v1/<username>/jobs/<job-id>
为什么这个请求会挂起,我该怎么做才能让它成功?
由于与问题无关的原因,我在设置 WebDriver 的功能时无法设置作业的名称。
Why does this request hang and what can I do to make it successful?
很可能在 client.SendAsync
方法中混合了异步调用和阻塞调用,例如 .Result
,这可能导致死锁,或者如您所说,导致代码挂起。
考虑使用 await
.
使方法调用一直异步
public async Task CallAPIAsync() {
using (var client = new HttpClient()) {
var sessionId = Browser.Driver.GetSessionId();
var uri = new Uri($"https://saucelabs.com/rest/v1/{Configuration.SauceUserName}/jobs/{sessionId}");
var uriWithCred =
new UriBuilder(uri) {
UserName = $"{Configuration.SauceUserName}",
Password = $"{Configuration.SauceAccessKey}"
}.Uri;
var payload = new StringContent($"{{\"name\":\"{TestMethodName}\"}}", Encoding.UTF8, "application/json");
var request = new HttpRequestMessage {
Method = HttpMethod.Put,
RequestUri = uriWithCred,
Content = payload
};
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
}
}
我正在尝试发送 PUT
请求以更新 Saucelabs via their API 上的职位。但是,以下代码挂起,我不确定为什么。
using (var client = new HttpClient())
{
var sessionId = Browser.Driver.GetSessionId();
var uri = new Uri($"https://saucelabs.com/rest/v1/{Configuration.SauceUserName}/jobs/{sessionId}");
var uriWithCred =
new UriBuilder(uri)
{
UserName = $"{Configuration.SauceUserName}",
Password = $"{Configuration.SauceAccessKey}"
}.Uri;
var payload = new StringContent($"{{\"name\":\"{TestMethodName}\"}}", Encoding.UTF8, "application/json");
var request = new HttpRequestMessage
{
Method = HttpMethod.Put,
RequestUri = uriWithCred,
Content = payload
};
var response = client.SendAsync(request).Result;
}
以下 cUrl 请求成功(当然是经过编辑的凭据)。
curl -X PUT -s -u <username>:<access-key>
-d "{\"name\": \"test name\"}"
https://saucelabs.com/rest/v1/<username>/jobs/<job-id>
为什么这个请求会挂起,我该怎么做才能让它成功?
由于与问题无关的原因,我在设置 WebDriver 的功能时无法设置作业的名称。
Why does this request hang and what can I do to make it successful?
很可能在 client.SendAsync
方法中混合了异步调用和阻塞调用,例如 .Result
,这可能导致死锁,或者如您所说,导致代码挂起。
考虑使用 await
.
public async Task CallAPIAsync() {
using (var client = new HttpClient()) {
var sessionId = Browser.Driver.GetSessionId();
var uri = new Uri($"https://saucelabs.com/rest/v1/{Configuration.SauceUserName}/jobs/{sessionId}");
var uriWithCred =
new UriBuilder(uri) {
UserName = $"{Configuration.SauceUserName}",
Password = $"{Configuration.SauceAccessKey}"
}.Uri;
var payload = new StringContent($"{{\"name\":\"{TestMethodName}\"}}", Encoding.UTF8, "application/json");
var request = new HttpRequestMessage {
Method = HttpMethod.Put,
RequestUri = uriWithCred,
Content = payload
};
var response = await client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
}
}