gpt4 book ai didi

asp.net - 在 Azure 上添加授权后,Post 方法返回 "The requested resource does not support http method ' GET'.\"

转载 作者:行者123 更新时间:2023-12-03 03:09:59 24 4
gpt4 key购买 nike

我们创建了 Web 服务,可以发布一些数据、修改数据并将其全部以 json 格式发回。入口方法有System.Web.Http.HttpPost属性

    [HttpPost]
public object Index(dynamic input)
{
// return modified data;
}

工作正常,但我们想进行一些自动测试。为了实现这一点,我们在 azure 上发布了网站。但是,为了限制访问,在服务上配置了 Azure Active Directory 身份验证。代码中没有进行任何更改。仅 Web 应用程序/API 应用程序使用 key 在 Active Directory 中注册。然后在服务认证/授权配置中添加客户端。

从使用 ADAL 库的代码中,我获得了访问 token 。使用它我可以成功调用 get 方法(服务还有另一个 get 条目,但具有不同的本地路径)

   HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
result.AccessTokenType, result.AccessToken);
var uriString = @"http://myService.azurewebsites.net/api/MyGetMethod";
Uri uri = new Uri(uriString);
HttpResponseMessage httpResponse = await httpClient.GetAsync(uri);
if (httpResponse.IsSuccessStatusCode)
{
return await httpResponse.Content.ReadAsStringAsync();
}

但是当我尝试用 post 方法做同样的事情时,我得到了回复:

{StatusCode: 405, ReasonPhrase: 'Method Not allowed' with message inside "{\"Message\":\"请求的资源不支持 http 方法 'GET'。\"}"

以下代码用于发布消息:

  HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(result.AccessTokenType, result.AccessToken);
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var stringContent = new StringContent("{MyData: 5.4}");
stringContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await httpClient.PostAsync("http://myService.azurewebsites.net/api/MyPostMethod", stringContent).Result;
if (response.IsSuccessStatusCode) {
return await response.Content.ReadAsStringAsync();}

最佳答案

根据描述,您已使用 Azure 广告保护 Web 服务,并且能够成功发送 GET 请求。

从错误消息来看,问题与 Web 服务有关。这是一个适合我的示例:

// POST api/values
public void Post([FromBody]Message value)
{

}

public class Message
{
public Double MyData;
}

如果问题仍然存在,您可以共享 Web 服务代码来帮助缩小问题范围。

更新

无论是否使用 Azure AD 进行身份验证,您上传的代码示例对我来说都很有效,请参阅下面的测试请求以查看是否有帮助:

enter image description here

要将 Web API 与 Azure AD 集成,您还可以引用代码示例 here .

关于asp.net - 在 Azure 上添加授权后,Post 方法返回 "The requested resource does not support http method ' GET'.\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39855983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com