Simpleworkerrequest
From msdn, we know that we can reload simpleworkerrequest to implement more of our own behaviors. How to overload simpleworkerrequest? From the source code of Cassini request. CS, we can see many methods and implementation methods, including
Sendresponsefrommemory (byte [] data, int length)
Sendresponsefromfile (string filename, long offset, long length)
Sendresponsefromfile (intptr handle, long offset, long length)
Sendresponsefromfilestream (filestream F, long offset, long length)
Flushresponse (bool finalflush)
I am most interested. Analyze the source code of request. cs. I know
Sendresponsefrommemory (byte [] data, int length)
Flushresponse (bool finalflush)
It should be my key consideration
The source code of myrequest. CS is as follows:
Public class myrequest: simpleworkerrequest
{
Private arraylist _ responsebodybytes;
Private system. Io. memorystream MEM;
Private system. Io. streamwriter writer;
Public myrequest (string pwebpage, string pquery, string pfile): Base (pwebpage, pquery, null)
{
This. mem = new memorystream ();
// This. Writer = new streamwriter (pfile, false, encoding. getencoding ("gb2312 "));
This. Writer = new streamwriter (pfile, false, encoding. utf8 );
This. _ responsebodybytes = new arraylist ();
This. initfilepath (pwebpage );
}
Private void initfilepath (string path)
{
This. _ Path = path;
String strvir = getapppathtranslated ();
String STR = strvir + path;
STR = Str. Replace ("/","\\");
This. _ filepath = STR;
}
Public override void sendresponsefrommemory (byte [] data, int length)
{
If (length> 0)
{
Byte [] bytes = new byte [length];
Buffer. blockcopy (data, 0, bytes, 0, length );
_ Responsebodybytes. Add (bytes );
}
}
Public override void flushresponse (bool finalflush)
{
For (INT I = 0; I <_ responsebodybytes. Count; I ++)
{
Byte [] bytes = (byte []) _ responsebodybytes [I];
This. mem. Write (bytes, 0, bytes. Length );
}
_ Responsebodybytes = new arraylist ();
If (finalflush)
{
System. Io. streamreader READ = new streamreader (this. MEm );
This. mem. Position = 0;
String STR = read. readtoend ();
This. Writer. Write (STR );
This. Writer. Close ();
}
}
Why does memorystream be used to read the string of the entire page and write it into the file? Why is it directly written to a file? This is due to encoding considerations. The encoding of the file content is not guaranteed. When you use iewebbrowser to open the file, garbled characters still occur, even though no garbled characters are displayed when you open the file with notepad. If you enable it using IE, you can see the same effect as Windows programs by adjusting the encoding used.
You may notice that the Code retains a gb2312 record to construct the streamwriter code. When using gb2312, there may be a strange phenomenon. If the ASPX page encoding is specified as charset = gb2312, the interface is normal; if it is charset = UTF-8, ie will naturally select utf8, and the page displayed in the result is garbled. It is normal only when it is gb2312. This problem does not exist if you use utf8 to construct streamwriter.