简体   繁体   English

发布请求C#之后获取回声值(WebClient UploadValues)

[英]Get echo value after post request C# (WebClient UploadValues)

I use Xamarin (C#) and PHP (with MySqli to connect to my database) to send data via POST request. 我使用Xamarin(C#)和PHP(通过MySqli连接到我的数据库)来通过POST请求发送数据。 I have a webclient where I send post request to PHP file for account creation and everything work fine but I need the PHP to echo a response after it received data (User already exist, user doen't exist... etc) and the C# receive it. 我有一个Web客户端,我在其中将发布请求发送到PHP文件以创建帐户,并且一切正常,但是我需要PHP在收到数据(用户已经存在,用户不存在...等)和C#后回显响应收到它。 Actually, my PHP print the response on his code via echo but I don't know how to check the state of the page as reaction after it received my data. 实际上,我的PHP通过echo将响应打印在他的代码上,但是我不知道如何在接收到我的数据后将页面状态作为响应进行检查。

                            using (WebClient client = new WebClient())
                        {
                            var data = new NameValueCollection();
                            data.Add("userid", userid.ToString());
                            data.Add("password", password);

                            byte[] response = client.UploadValues(Url, data);
                            string rep = client.DownloadString("http://xxxx.fr/CreateProfile.php");
                            Toast.MakeText(this, rep, ToastLength.Short).Show();
                        }

Actually my request is sent and ONLY AFTER, it download the code so I have the error message "Student doesn't exist" everytime. 实际上,我的请求已发送,并且仅在此之后,它下载了代码,因此每次都出现错误消息“学生不存在”。 Otherwise, when I try to use it via a simple form it works. 否则,当我尝试通过简单的形式使用它时,它将起作用。 So the error is not in the PHP because the request is well executed on my database but I just ask for the page's code too late an without any parameter.. 因此错误不在PHP中,因为该请求已在我的数据库中很好地执行,但是我只是要求页面代码太晚而没有任何参数。

Here is my PHP code: 这是我的PHP代码:

if ($data === "404"){
    echo "Student doesn't exist";
    }

else{
$checkIfExist= "SELECT name FROM users WHERE id = $user_id";
    if($result = $conn->query($checkIfExist)) {
        $row_cnt = $result->num_rows;
              } 

    if ($row_cnt > 0){
        echo "User already exist.";
    }

    else{
        echo"New user";
    }

So I want to know if I can get the echo returned when I send the values using WebClient or if I need to use something else like HttpClient or HttpRequest... 所以我想知道当我使用WebClient发送值时是否可以获得返回的回显信息,或者是否需要使用其他诸如HttpClient或HttpRequest之类的东西。

Solution was so simple, thanks to Jason 有了Jason,解决方案非常简单

use Encoding.ASCII.GetString(response) 使用Encoding.ASCII.GetString(response)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM