我正在尝试使用PUT方法使用Odata服务更新记录。以下是执行此操作的代码
private async Task<HttpResponseMessage> PutJsonAsync(string messageBody,string B_Code)
{
string userName = ConfigurationManager.AppSettings["Username"];
string password = ConfigurationManager.AppSettings["Password"];
string BaseURL = ConfigurationManager.AppSettings["BaseURL"];
try
{
using (var httpClient = new HttpClient())
{
var request = new StringContent(messageBody, Encoding.UTF8, "application/json");
string apiUrl = "SAM('" + B_Code + "')";
request.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Headers.TryAddWithoutValidation("If-Match", "*");
var url = string.Format("{0}{1}", BaseURL, apiUrl);
var creds = userName + ":" + password;
var credentials = Encoding.ASCII.GetBytes(creds);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(credentials));
response = await httpClient.PutAsync(new Uri(url), request);
}
}
catch (Exception ex)
{
throw ex;
}
return response;
}
我正在设置标头If-Match,这个PUT方法需要工作。 但是当我调试它时会在响应中抛出错误响应501
response {StatusCode: 501, ReasonPhrase: 'Not Implemented', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
OData-Version: 4.0
Date: Mon, 30 Oct 2017 20:42:28 GMT
Set-Cookie: JSESSIONID=XYZ; Path=/; Secure; HttpOnly
Set-Cookie: XYZ;PATH=/;SECURE;HTTPONLY
Server: Apache-Coyote/1.1
Vary: Accept-Encoding
Connection: keep-alive
Content-Length: 277
Content-Type: application/json; odata.metadata=minimal
}} System.Net.Http.HttpResponseMessage
但是在POSTMAN中尝试相同的PUT方法可以正常工作而不会出现任何错误。验证了messageBody是有效的JSON。不确定可能是什么问题。