Screen scrape to get HTML page from client and send to database as binary-return again


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. System.Net.WebClient myClient = new System.Net.WebClient();
  2. byte[] requestHTML;
  3. //setup credentials to do the double hop between the server and client
  4. System.Security.Principal.WindowsImpersonationContext impersonationContext = default(System.Security.Principal.WindowsImpersonationContext);
  5. System.Security.Principal.WindowsIdentity currentIdentity = (System.Security.Principal.WindowsIdentity)User.Identity;
  6. impersonationContext = currentIdentity.Impersonate();
  7. myClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
  8.  
  9. requestHTML = myClient.DownloadData(Request.Url.ToString());
  10. int pageID = bis.IntInsertPage(appraisalID, requestHTML);//Insert the page in the database and get the pageID
  11.  
  12. // Revert back to original context of the ASP.NET process
  13. impersonationContext.Undo();
  14.  
  15.  
  16. DbCommand command = dbSQL.GetStoredProcCommand("dbo.procInsertPage");
  17. dbSQL.AddInParameter(command, "AppraisalID", DbType.Int32, appraisalID);
  18. dbSQL.AddInParameter(command, "Page", DbType.Binary, page);
  19. dbSQL.AddOutParameter(command, "PageID", DbType.Int32, 4);
  20. try
  21. {
  22. dbSQL.ExecuteNonQuery(command);
  23. return Convert.ToInt32(dbSQL.GetParameterValue(command, "@PageID"));
  24. }
  25.  
  26.  
  27.  
  28. //return binary data so we can display
  29. //convert the binary back to a string containing the original HTML
  30. string htmlPage = ConvertBinaryToString((byte[])page);
  31.  
  32. //decode the page
  33. string decHtmlPage = Server.HtmlDecode(htmlPage);
  34.  
  35. // display the retrieved page in a literal control
  36. litPage.Text = decHtmlPage;
  37.  
  38. //Method to convert binary to string
  39. protected string ConvertBinaryToString(Byte[] input)
  40. {
  41. System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
  42. String str = enc.GetString(input);
  43. return str;
  44. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.