/// <Summary>
/// Get the Page code
/// </Summary>
/// <Param name = "response"> httpwebresponse </param>
/// <Returns> </returns>
Private encoding getpageencoding (httpwebresponse response)
{
// If the Content-Type header is found
String ctype = response. headers ["Content-Type"];
String charset = string. empty;
If (! String. isnullorempty (ctype ))
{
Int S = ctype. indexof ("charset = ");
If (S>-1)
{
Charset = ctype. substring (S + 8); // because the length of "charset =" is 8 characters
}
}
// If no Content-Type is found, you have to search for it from the script.
If (string. isnullorempty (charset ))
{
Httpwebrequest all_coderequest = (httpwebrequest) webrequest. Create (URL );
Httpwebresponse all_coderesponse = (httpwebresponse) all_coderequest.getresponse ();
If (all_coderesponse.statuscode = httpstatuscode. OK)
{
Streamreader the_reader = new streamreader (all_coderesponse.getresponsestream (), encoding );
/// All Page code text
String all_code = the_reader.readtoend ();
If (! String. isnullorempty (all_code ))
{
Int S = all_code.indexof ("charset = ");
Int e =-1;
If (S>-1)
{
S = S + 8;
E = all_code.indexof ("/" ", S + 1 );
If (E>-1)
{
Charset = all_code.substring (S, e-S );
/// Remove the quotation marks from the start position
Charset = charset. trimstart (New char [] {'"'});
/// Remove the ending quotation marks
Charset = charset. trimend (New char [] {'> ','"'});
}
}
}
The_reader.close ();
The_reader.dispose ();
}
All_coderesponse.close ();
}
If (! String. isnullorempty (charset ))
{
Try
{
Encoding = encoding. getencoding (charset );
}
Catch (exception)
{
Encoding = encoding. utf8;
}
}
Return encoding;
}
Public void SaveFile ()
{
String all_code = "";
Try
{
Httpwebrequest all_coderequest = (httpwebrequest) webrequest. Create (URL );
Httpwebresponse all_coderesponse = (httpwebresponse) all_coderequest.getresponse ();
If (all_coderesponse.statuscode = httpstatuscode. OK)
{
Encoding = getpageencoding (all_coderesponse );
Streamreader the_reader = new streamreader (all_coderesponse.getresponsestream (), encoding );
All_code = the_reader.readtoend ();
Filestream FS = new filestream ("F: // test.html", filemode. Create );
Streamwriter Sw = new streamwriter (FS, encoding );
Sw. writeline (all_code );
Sw. Close ();
FS. Close ();
The_reader.close ();
The_reader.dispose ();
All_coderesponse.close ();
}
}
Catch (exception)
{
Throw;
}
}