为什么Web页面的WebResponse有尾随空格?

时间:2012-11-12 09:46:19

标签: php .net webrequest streamreader webresponse

我在网站上写了一个PHP页面,以OK回复。在PHP中,已完成以下操作:

echo 'OK';

当我在.NET中请求页面并将响应读入字符串时,结果字符串有一个尾随空格,因此它是'OK '。字符串的长度为3个字符。为什么会这样,我该如何解决?

.NET代码为:

Private Function GetStrFromPostRequest(ByVal url As String, ByVal encoding As Text.Encoding, ByVal params As String) As String

    Dim strWebResponse As String
    Dim Request As HttpWebRequest = WebRequest.Create(url)
    Request.Method = "POST"
    Request.ContentType = "application/x-www-form-urlencoded"
    Using writer As StreamWriter = New StreamWriter(Request.GetRequestStream(), encoding)
        writer.Write(params)
    End Using
    Using Response As WebResponse = Request.GetResponse
        Using reader As StreamReader = New StreamReader(Response.GetResponseStream(), encoding)
            strWebResponse = reader.ReadToEnd
        End Using
    End Using

    Return strWebResponse

End Function

1 个答案:

答案 0 :(得分:0)

尝试将PHP来源更改为:

<?php echo 'OK';

那应该删除空间。任何以PHP代码结尾的PHP文件都不应该有一个结束?>,因为它只会导致恼人的问题。