HttpClient始终在Xamarin类的调用时返回状态代码200

时间:2017-07-30 18:15:30

标签: php xamarin http-status-codes

我正在学习xamarin和php并且我被困在这里,因为我无法从php收到正确的回复,我知道这是一个错误,因为我故意拼错了查询。

我使用XAMPP和Visual Studio 2017,我在php中的代码如下所示:

<?php
require_once('logger.php');
include('userobj.php');
$log = new Log("errors.log");


if($_SERVER['REQUEST_METHOD'] === 'POST')
{
   $log->{'Write'}('Received a POST Request');
    try
    {

        $data = json_decode(file_get_contents('php://input'), true);

        foreach($data as $key =>$value)
        {
            $log->{'Write'}("array key: " . $key . " array value: " . $value);
        }

        $user = new User($data);
        $log->{'Write'}("after createad: ");
        $user->{'CreateNewUser'}();

    }
    catch (Exception $e)
    {
        http_response_code("400");
        $log->{'Write'}("Error message\n"+$e->getMessage());
    }

}
else
{    
    $log->{'Write'}("REQUEST NOT A POST");
}


?>

我已尝试在php中使用Try / Catch并且我知道它收到错误因为我能够将其写入我的日志,我调用的方法看起来像这样:

public function CreateNewUser()
    {
        try
        {
             global $log, $config;
            $log->{'Write'}("Creating Connection"); 
            $connection = mysqli_connect($config->db_host,$config->db_username,$config->db_pwd,$config->db_dbname);

            if($connection)
            {
                $log->{'Write'}("Creating new user");       
                $query = "INSERT INTOS users (name, lastname, policynumber, phone, email) ";
                $query .= "VALUES ('$this->name', '$this->lastname', '$this->policynumber', '$this->phone', '$this->email');";
                $result = mysqli_query($connection, $query);
                if(!$result)
                {
                    $log->{'Write'}('Query Failed on Create New User: ');
                    $log->{'Write'}(mysqli_error($connection));
                    die('Query Failed' . mysqli_error($connection));
                }
            }
            else
            {
                $log->{'Write'}("Connection Failed");
                die('Connection Failed');
            }
        }
        catch(Exception $e)
        {
            http_response_code("400");
            throw $e;
        }


    }

正如我所提到的,我拼错了查询以强制出错...最后我来自Xamarin c#的电话看起来像这样:

public static async Task CreateUser(User user)
        {
            try
            {

                HttpClient _client = new HttpClient();
                var content = JsonConvert.SerializeObject(user);
                var response = await _client.PostAsync(url, new StringContent(content));
                string error = response.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

如果我调试此代码并查看变量的值&#34;响应&#34;它总是有状态代码200,我见过类似的问题,但他们都使用&#34; GET&#34;。

1 个答案:

答案 0 :(得分:0)

使用邮递员按照Jason的建议在评论中帮助我看到问题,问题在于这段代码:

if($connection)
            {
                $log->{'Write'}("Creating new user");       
                $query = "INSERT INTOS users (name, lastname, policynumber, phone, email) ";
                $query .= "VALUES ('$this->name', '$this->lastname', '$this->policynumber', '$this->phone', '$this->email');";
                $result = mysqli_query($connection, $query);
                if(!$result)
                {
                    $log->{'Write'}('Query Failed on Create New User: ');
                    $log->{'Write'}(mysqli_error($connection));
                    die('Query Failed' . mysqli_error($connection));
                }
            }

问题是我已经将查询结果存储在名为$ result的变量中,然后我在if语句中使用了它,所以我已将代码更新为:

if(!$result)
                {
                    $log->{'Write'}('Query Failed on Create New User: ');
                    $log->{'Write'}(mysqli_error($connection));
                    http_response_code(500); //This line returns the response code 500
                    die('Query Failed ' . mysqli_error($connection));
                }

我在移动设备的通话过程中无法看到此行为,因此使用&#39; Postman&#39;非常有用。

相关问题