asp.net-mvcrestapihttpresponsemessage

Failure to getting response from post request in HttpResponseMessage


I read a lot of questions and tested a lot of code but I couldn't solve my problem. I want call rest API (that write with php code) in asp controller.

I tested my web service with postman app and get responds correctly from web service . But I can't get the right answer through asp request.

My problem: I get status code 200 for response(in asp controller) But the instructions on the web service are not executed(for example: don't register user). Where can I find the problem and get the data in the answer?

 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));
        }
    }

call Rest Api from Postman


Solution

  • I successful finding answer of my question. I change only URI of base address and set by this: "domainurl/shopktphp/" and set Post json async by "register.php"

    client.BaseAddress = new Uri("domainurl/shopktphp/");
    
    HttpResponseMessage response = await client.PostAsJsonAsync("register.php", json.Data);