无法从 HttpResponseMessage 中的 post 请求获得响应
Failure to getting response from post request in HttpResponseMessage
我看了很多问题,测试了很多代码,但还是无法解决我的问题。
我想在 asp 控制器中调用 rest API(用 php 代码编写)。
我用 postman 应用程序测试了我的网络服务,并从网络服务得到了正确的响应。但是我无法通过asp请求得到正确的答案。
我的问题:我收到状态代码 200 的响应(在 asp 控制器中)但是 Web 服务上的指令没有执行(例如:不注册用户)。我在哪里可以找到问题并获取答案中的数据?
public async Task registerInShop()
{
try
{
ShopUserModel model = new ShopUserModel()
{
user_login = "ff",
user_pass = "v12315",
user_nicename = "",
user_url = "",
user_registered = "",
user_activation_key = "",
user_status = 0,
display_name = "ff",
};
JsonResult json = Json(model, JsonRequestBehavior.AllowGet);
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("domainurl/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
HttpResponseMessage response = await client.PostAsJsonAsync("shopktphp/register.php", json.Data);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{ // Get the URI of the created resource.
Uri returnUrl = response.Headers.Location;
var res = response.Content;
Console.WriteLine(returnUrl);
}
}
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
}
}
我成功找到了问题的答案。我只更改基地址的 URI 并按此设置:"domainurl/shopktphp/"
并通过 "register.php"
设置 Post json 异步
client.BaseAddress = new Uri("domainurl/shopktphp/");
HttpResponseMessage response = await client.PostAsJsonAsync("register.php", json.Data);
我看了很多问题,测试了很多代码,但还是无法解决我的问题。 我想在 asp 控制器中调用 rest API(用 php 代码编写)。
我用 postman 应用程序测试了我的网络服务,并从网络服务得到了正确的响应。但是我无法通过asp请求得到正确的答案。
我的问题:我收到状态代码 200 的响应(在 asp 控制器中)但是 Web 服务上的指令没有执行(例如:不注册用户)。我在哪里可以找到问题并获取答案中的数据?
public async Task registerInShop()
{
try
{
ShopUserModel model = new ShopUserModel()
{
user_login = "ff",
user_pass = "v12315",
user_nicename = "",
user_url = "",
user_registered = "",
user_activation_key = "",
user_status = 0,
display_name = "ff",
};
JsonResult json = Json(model, JsonRequestBehavior.AllowGet);
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("domainurl/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
HttpResponseMessage response = await client.PostAsJsonAsync("shopktphp/register.php", json.Data);
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{ // Get the URI of the created resource.
Uri returnUrl = response.Headers.Location;
var res = response.Content;
Console.WriteLine(returnUrl);
}
}
}
catch (Exception ex)
{
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(ex));
}
}
我成功找到了问题的答案。我只更改基地址的 URI 并按此设置:"domainurl/shopktphp/" 并通过 "register.php"
设置 Post json 异步client.BaseAddress = new Uri("domainurl/shopktphp/");
HttpResponseMessage response = await client.PostAsJsonAsync("register.php", json.Data);