如何删除最后一行 space streamwriter?
How to remove last line space streamwriter?
目前我在创建文件时遇到问题,我正在尝试使用 streamWriter class 编写文本内容,但我没有得到预期的答案..
下面是我的示例代码:-
我的 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
我只想在同一行而不是新行中使用该标记参数..
谢谢。
如果您只想删除接收字节末尾的换行符,请像这样更改 while
循环中的代码:
while ((bytes = receiveStream.Read(buff, 0, 1024)) > 0)
{
if (buff[bytes-1] == 0x0a)
bytes -= 1;
//Write the stream directly to the client
context.Response.OutputStream.Write(buff, 0, bytes);
context.Response.Write("&token="+token);
}
几个注意事项:
- 只有当
0x0a
(换行字节,'\n'
作为一个字符)位于您 收到的 字节的末尾时,它才会起作用。如果出于某种原因,服务器发送的消息在几个块中被接收,您首先必须确保在检查最后一个字节之前接收到所有要接收的信息。
另请注意,这会导致当前代码中出现多个 &token=...
行。
- 根据服务器的不同,它可能使用回车符 return(
0x0d
或 '\r'
)作为其行结束字节,甚至两者都使用。检查服务器发送的内容并相应地调整代码。
目前我在创建文件时遇到问题,我正在尝试使用 streamWriter class 编写文本内容,但我没有得到预期的答案.. 下面是我的示例代码:-
我的 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
我只想在同一行而不是新行中使用该标记参数..
谢谢。
如果您只想删除接收字节末尾的换行符,请像这样更改 while
循环中的代码:
while ((bytes = receiveStream.Read(buff, 0, 1024)) > 0)
{
if (buff[bytes-1] == 0x0a)
bytes -= 1;
//Write the stream directly to the client
context.Response.OutputStream.Write(buff, 0, bytes);
context.Response.Write("&token="+token);
}
几个注意事项:
- 只有当
0x0a
(换行字节,'\n'
作为一个字符)位于您 收到的 字节的末尾时,它才会起作用。如果出于某种原因,服务器发送的消息在几个块中被接收,您首先必须确保在检查最后一个字节之前接收到所有要接收的信息。 另请注意,这会导致当前代码中出现多个&token=...
行。 - 根据服务器的不同,它可能使用回车符 return(
0x0d
或'\r'
)作为其行结束字节,甚至两者都使用。检查服务器发送的内容并相应地调整代码。