Monday, November 14, 2016

Call Web Request method for Authorization process using C#

 private string WebRequestAuthorization(string url)
        {
            string jsonResponse = string.Empty;
            try
            {
                var webRequest = System.Net.WebRequest.Create(url);
                if (webRequest != null)
                {
                    webRequest.Proxy = new WebProxy(this.WebProxyUrl, true);
                    webRequest.Proxy.Credentials = CredentialCache.DefaultCredentials;
                    webRequest.Method = "GET";
                    webRequest.Headers.Add("Authorization", "");
                    using (System.IO.Stream s = webRequest.GetResponse().GetResponseStream())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(s))
                        {
                            jsonResponse = sr.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error while getting json response using web request", ex);
                throw ex;
            }
            return jsonResponse;
        }

Get Bytes using Web Request Method C#

//Get Bytes using Web Request Method C#
//This is the method which is used for large data
        private byte[] WebRequestOnHeaderByte(string url)
        {
            byte[] buffer = null;
            int bytesProcessed = 0;
            Stream remoteStream = null;
            WebResponse response = null;
            MemoryStream memorystream = null;
            try
            {
                WebRequest request = WebRequest.Create(url);
                if (request != null)
                {
                    request.Proxy = new WebProxy(this.WebProxyUrl, true);
                    request.Proxy.Credentials = CredentialCache.DefaultCredentials;
                    request.Method = "GET";
                    response = request.GetResponse();
                    memorystream = new MemoryStream();
                    if (response != null)
                    {
                        remoteStream = response.GetResponseStream();
                        buffer = new byte[1024];
                        int bytesRead;
   //This is loop which read 1KB bytes and add into memory stream and you can also add this stream into local file.
                        do
                        {
                            bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
                            memorystream.Write(buffer, 0, bytesRead);
                            bytesProcessed += bytesRead;
                        }
                        while (bytesRead > 0);
                    }
                    buffer = memorystream.ToArray();
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Error while getting bytes using web request", ex);
                throw ex;
            }
            return buffer;
        }

Remove Html Tag like class,achor tag from HTMLAgilityPack

//Class for creating attachment public class Attachment { ///


/// Gets or sets the attachment content ///public byte[] Content { get; set; } }
result = System.Text.Encoding.UTF8.GetString(attachment.Content);

HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument(); //Load html

html.LoadHtml(result); //Remove html tag like css class,anchor tag (any link here) using Linq and finding css class

html.DocumentNode.Descendants().Where(x => (x.Attributes["class"] != null && x.Attributes["class"].Value.Contains("ClassName"))).ToList().ForEach(n => n.Remove());