我將TypeScript應用程序部分移植到C#中。應用程序需要在Web服務上執行某個REST調用。 web服務是用C#和具體方法有以下特徵:添加HttpClient PUT參數的正確方法? - 將TypeScript REST調用移植到C#
public async Task<IHttpActionResult> RegisterDevice(DeviceRegistration deviceRegistration)
原來的應用程序(打字稿)並以下列方式REST調用:
this.$http.put(this.Configuration.registrationEndpointUrl, {
field1: value1,
field2: value2,
field3: value3
})
我搜索的StackOverflow如何打電話,找到了解釋如何做的帖子。然後我創建了C#版本如下:
var pairs = new List<KeyValuePair<string, string>>() {
new KeyValuePair<string, string>("field1", Settings.Value1),
new KeyValuePair<string, string>("field2", Settings.Value2),
new KeyValuePair<string, string>("field3", "value3")
};
var content = new FormUrlEncodedContent(pairs);
var response = await Client.PutAsync(Configuration.RegistrationUrl, content);
然而,這只是返回一個空的響應。我在想FormUrlEncodedContent
有什麼不對嗎?在C#中執行此調用的正確方法是什麼?
編輯:客戶端初始化代碼:
private static async Task<HttpClient> CreateClient() {
await Authenticator.VerifyAuthentication();
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("x-zumo-auth", Settings.OAuthToken);
client.DefaultRequestHeaders.Add("x-oauth-provider", Settings.OAuthProvider.Value.ToString());
return client;
}
什麼是響應的狀態碼? – Vyrira
@Vyrira BadRequest – EpicSam