Tuesday, December 7, 2010

Read html and Wrtie to HTMl fiile

public static bool WriteHtmlFile(string pHtmlFileNameWithFullPath, string pValue)
{
bool mSuccess = false;

if (pValue.Trim().Length > 0)
{

try
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(pHtmlFileNameWithFullPath, false);

sw.Write(pValue);

sw.Close();
}
catch (Exception)
{
Exception ex = new Exception(pHtmlFileNameWithFullPath.Substring(pHtmlFileNameWithFullPath.LastIndexOf("\\")) + " not accessible !! contact administrator..");
throw ex;
}

mSuccess = true;
}

return mSuccess;
}

private string GetHTML1(string url, string resourceName)
{
try
{
StringBuilder content = new StringBuilder();

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (StringWriter writer = new StringWriter(content))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8))
{

writer.Write(reader.ReadToEnd());
reader.Close();
}

writer.Close();
}

return content.ToString();
}
catch (Exception ex)
{
throw ex;
}
}

public string GetAbsoluteURI(string virtualPath, string queryString, bool useSecure)
{
try
{
string path = VirtualPathUtility.ToAbsolute(virtualPath);
Uri newUri = new Uri(HttpContext.Current.Request.Url, path);

UriBuilder uriBuilder = new UriBuilder(newUri);

if (!string.IsNullOrEmpty(queryString))
{
if (queryString.StartsWith("?")) queryString = queryString.Substring(1);
uriBuilder.Query = queryString;
}

if (!useSecure)
{
uriBuilder.Scheme = "http";
if (uriBuilder.Port == 443) uriBuilder.Port = 80;
}

return uriBuilder.Uri.ToString();
}
catch
{
return string.Empty;
}
}

No comments:

Post a Comment