我目前所面临的问题而创建的文件,我试图使用StreamWriter类来书写文字内容,但我没有得到预期的答案.. 下面是我的示例代码: -如何删除最后一行空格streamwriter?
我的C#代码如下所示: - 上面代码的
public void ProcessRequest(HttpContext context)
{
// Create a connexion to the Remote Server to redirect all requests
RemoteServer server = new RemoteServer(context);
// Create a request with same data in navigator request
HttpWebRequest request = server.GetRequest();
// Send the request to the remote server and return the response
HttpWebResponse response = server.GetResponse(request);
context.Response.AddHeader("Content-Disposition", "attachment; filename=playlist.m3u8");
context.Response.ContentType = response.ContentType;
Stream receiveStream = response.GetResponseStream();
var buff = new byte[1024];
int bytes = 0;
string token = Guid.NewGuid().ToString();
while ((bytes = receiveStream.Read(buff, 0, 1024)) > 0)
{
//Write the stream directly to the client
context.Response.OutputStream.Write(buff, 0, bytes);
context.Response.Write("&token="+token);
}
//close streams
response.Close();
context.Response.End();
}
输出如下: -
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=20776,CODECS="avc1.66.41",RESOLUTION=320x240
chunk.m3u8?nimblesessionid=62
&token=42712adc-f932-43c7-b282-69cf349941da
但我的预期成果是: -
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:BANDWIDTH=20776,CODECS="avc1.66.41",RESOLUTION=320x240
chunk.m3u8?nimblesessionid=62&token=42712adc-f932-43c7-b282-69cf349941da
我只是想在同一行,而不是新的行令牌PARAM ..
谢谢。
您是否检查过从服务器收到的数据还没有以换行符结束?如果是这样,您需要在写入'context.Response'之前将其过滤掉。 – jcb
@jcb我以字节为单位获取数据,因此如何在将文件写入文件之前对其进行过滤? 你能给我一个示例代码吗? – IPS
你可以像'if(buff [bytes-1] == 0x0a)bytes - = 1;'这样检查是否换行符('\ n'如果一个字符,0x0a是十六进制值)是你收到的最后一个字节,然后就像你收到一个字节一样。但是,根据您的服务器,您可能会收到一个回车字节('\ r'或0x0d),或者甚至两者都作为消息的结尾。根据需要修改:) – jcb