HttpWebResponse没有从PHP响应

时间:2018-07-04 22:54:28

标签: c# php http

所以我在C#中有这段代码。

    private void SendMessage()
{
    // this is what we are sending
    string post_data = "loginIdPost=" + 2 + "&" + "contentPost=" + absenceInputField.text;

    string uri = "<URI IS HERE>";

    HttpWebRequest request = (HttpWebRequest)
    WebRequest.Create(uri); request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;
    request.Method = "POST";

    byte[] postBytes = Encoding.ASCII.GetBytes(post_data);


    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = postBytes.Length;
    Stream requestStream = request.GetRequestStream();

    requestStream.Write(postBytes, 0, postBytes.Length);
    requestStream.Close();


    HttpWebResponse response = (HttpWebResponse)request.GetResponse();




    Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
    Debug.Log(new StreamReader(response.GetResponseStream()).ReadToEnd());

    Debug.Log(response.StatusCode);
    Console.WriteLine(response.StatusCode);
}

它将一些信息发送到PHP文件,然后将数据放入我的数据库。数据已成功发送,但是HttpWebResponse变回空白。它返回一个空字符串“”。

这是我的PHP代码。

    <?php

//Id
if (isset($_POST["loginIdPost"]))
{
  $loginId = $_POST["loginIdPost"];
} 
else 
{
  $loginId = null;
}


//Content
if (isset($_POST["contentPost"]))
{
  $content = $_POST["contentPost"];
} 
else 
{
  $content = null;
}

try 
{
    $conn = new PDO("Connection String");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}
catch (PDOException $e) 
{
    print("Error connecting to SQL Server.");
    die(print_r($e)); 
}

$sth = $conn->prepare('Sql Query is here');
$sth->execute('details of execution are here');
echo "Success";

//Check Connection
if(!$conn)
{
    die("Connection Failed. ". mysqli_connect_error());
}
?> 

有人希望我返回“成功”时,有人知道为什么我要返回一个空字符串吗?同样,由于SQL查询确实将数据插入到我的数据库中,因此php代码中没有错误。

1 个答案:

答案 0 :(得分:0)

我设法通过删除Console.Writeline对其进行了修复。