Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#从PHP POST响应中逐行读取_C#_Php - Fatal编程技术网

C#从PHP POST响应中逐行读取

C#从PHP POST响应中逐行读取,c#,php,C#,Php,我想解析PHP响应中的每一行。 我试过: PHP: echo "Line 1\n"; echo "Line 2\n"; echo "Line 3\n"; C# 我得到每行中的每个回声,但我不能用它来单独处理 当我这样做的时候 C# 我只得到第一行,下一个循环返回null。 如何分别获取每一行?可能是因为\n,windows使用\r\n作为行终止符。尝试将\n替换为\r\n。不要为每次迭代创建流读取器 var reader = new StreamReader(response.GetRespo

我想解析PHP响应中的每一行。 我试过:

PHP:

echo "Line 1\n";
echo "Line 2\n";
echo "Line 3\n";
C#

我得到每行中的每个回声,但我不能用它来单独处理

当我这样做的时候

C#

我只得到第一行,下一个循环返回null。
如何分别获取每一行?

可能是因为
\n
,windows使用
\r\n
作为行终止符。尝试将
\n
替换为
\r\n
。不要为每次迭代创建流读取器

var reader = new StreamReader(response.GetResponseStream());
do
{
    client.addLine(reader.ReadLine());

}  while (!reader.EndOfStream);

假设您控制php输出,如果可能的话,使用JSON在php和C#之间进行通信可能会更容易,因为生成和解析JSON要简单得多。

谢谢您的建议,但我更喜欢php,因为我只需要它连接到数据库。我在C#做的其他事情。虽然我以后可能会重新考虑。是的,你对“\r\n”+这段代码很好用,甚至比我的代码还要短。
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadLine();
do
{
    client.addLine(responseString);
    responseString = new StreamReader(response.GetResponseStream()).ReadLine();
}   while (responseString != null);
var reader = new StreamReader(response.GetResponseStream());
do
{
    client.addLine(reader.ReadLine());

}  while (!reader.EndOfStream);