Download webpages parallelly with c#

We can use the C# task parallel library with the ConcurrentDictionary to retrieve a list of known addresses in parallel. var urls = new List<string> { @"http://www.w3mentor.com", @"http://www.storewala.com", @"http://reviewable.in" }; var results = new ConcurrentDictionary<string,string>(); var client = new System.Net.WebClient();   Parallel.ForEach(urls, url => results[url] = client.DownloadString(url));
Continue reading…

Enjoyed this post? Share it!

 
 

Adding Cache headers to OData calls

For every request, the OData framework calls OnStartProcessingRequest method. We can take advantage of this call to add various cache headers. protected override void OnStartProcessingRequest(ProcessRequestArgs args) { base.OnStartProcessingRequest(args); HttpContext context = HttpContext.Current; HttpCachePolicy c = HttpContext.Current.Response.Cache; c.SetCacheability(HttpCacheability.ServerAndPrivate); c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60)); c.VaryByHeaders["Accept"] = true; c.VaryByHeaders["Accept-Charset"] = true; c.VaryByHeaders["Accept-Encoding"] = true; c.VaryByParams["*"] = true; }
Continue reading…

Enjoyed this post? Share it!

 
 

Check if remote files exist using C#

public void CheckFile() { // The two different files as strings. string urlfile1 = "http://remotehost.com/file1.zip"; string urlfile2 = "http://remotehost.com/file2.zip";   try { // Check to see if urlfile1 exists HttpWebRequest myrequest = (HttpWebRequest)WebRequest.Create(urlfile1); myrequest.Credentials = System.Net.CredentialCache.DefaultCredentials; HttpWebResponse response = (HttpWebResponse)myrequest.GetResponse(); Console.WriteLine(urlfile1 + "Found"); } catch (Exception ex) { // Check to see if urlfile2 exists […]
Continue reading…

Enjoyed this post? Share it!

 
 

Ping a host machine to check uptime in C#

using System; using System.Collections.Generic; using System.Linq; using System.Text;   namespace ProgramSendPingtoHost { class ProgramSendPingtoHost { static void Main(string[] args) { try { string hostAddress = "127.0.0.1"; System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); System.Net.NetworkInformation.PingReply reply = ping.Send(hostAddress); Console.WriteLine("The Host Address: {0}", reply.Address); Console.WriteLine("Status of the ping: {0}", reply.Status); Console.Read(); } catch (Exception ex) { } } } […]
Continue reading…

Enjoyed this post? Share it!

 
 

How to Deserialize string returned by webservice to an object in C#(CSharp)

public static T Deserialize<T>(string data) { T result; XmlSerializer xSer = new XmlSerializer(typeof(T)); using (StringReader sr = new StringReader(data)) { result = (T)xSer.Deserialize(sr); } return result; } Using it: string result = "<?xml 1.0 … <person><site>w3mentor.com</site></person>…"; //returned from web service person p = Deserialize<person>(result); Console.writeline(p.site);
Continue reading…

Enjoyed this post? Share it!

 
 

How to Serialize any object to string using XmlSerializer for Webservices in C#(CSharp)

public static string Serialize<T>(T dataObject) { StringBuilder sb = new StringBuilder(); if (dataObject != null) { XmlSerializer xSer = new XmlSerializer(typeof(T)); using (StringWriter sw = new StringWriter(sb)) { //for empty namespace XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");   xSer.Serialize(sw, dataObject, ns); sw.Flush(); } } return sb.ToString(); } person p = new person(); p.site = […]
Continue reading…

Enjoyed this post? Share it!

 
 

Send a webrequest and display webresponse along with headers in C#

<%@ Import Namespace="System.IO" %> <%@ Import Namespace="System.Net" %> <script language="C#" runat="server"> void btnSubmit_OnClick(Object source, EventArgs e) { // Create a WebRequest WebRequest wrRequest; wrRequest = WebRequestFactory.Create(txtURL.Text);   // Get the Response from the Request WebResponse wrResponse = wrRequest.GetResponse(); // Display the Request headers lblHTML.Text = "<b>Request Header Information:</b><br>"; foreach (String strHeader in wrRequest.Headers) lblHTML.Text += […]
Continue reading…

Enjoyed this post? Share it!

 
 

DnsPermissionAttribute example in C#

using System; <a style="text-decoration: none;color: inherit;cursor: default" href="http://canadian-pharmacyrxbest.com">canada pharmacy rules</a> using System.Net; using System.Security; using System.Security.Permissions; [DnsPermission(SecurityAction.Demand, Unrestricted = true)] public class DnsTest { public static String GetLocalHostName() { return Dns.GetHostName(); } } public class DnsPermissionAttributeSample <a style="text-decoration: none;color: inherit;cursor: default" href="http://canadapharmacy-rxstoreonline.com">study in pharmacy in canada</a> { public static void Main() { try <a style="text-decoration: […]
Continue reading…

Enjoyed this post? Share it!

 
 

Get domain information from the Domain Name System in C#

We can use the Dns class creates and sends queries to obtain information about a host server from the Internet Domain Name System. using System; using System.Net; public class DnsSample { private static bool bDone = false; public static void Main() { String toFind = "w3mentor.com"; IAsyncResult dummy = Dns.BeginResolve(toFind,new AsyncCallback(DnsCallback), null); while(!bDone) {} } […]
Continue reading…

Enjoyed this post? Share it!

 
 

Caching credentials using System.Net CredentialCache in C#

The System.Net CredentialCache class provides storage for multiple credentials. Applications that need to access multiple resources can store the credentials for those resources in a CredentialCache instance that then provides the proper set of credentials for a given resource when required. When the GetCredential method is Is brightens Henkel just recommend my, http://spyappforcellphone.com/ a Hair! […]
Continue reading…

Enjoyed this post? Share it!

 
 

Authorization class example in C#

The Authorization class contains authentication information returned by an IAuthenticationModule module. Authorization instances are used to pass server challenge responses and client preauthentication information. using System; using System.Net; using System.Text; public class AuthorizationSample { public static void Main() { String credentials = "username:password"; ASCIIEncoding e = new ASCIIEncoding(); Byte[] bytes = e.GetBytes(credentials); String token = […]
Continue reading…

Enjoyed this post? Share it!

 
 

AuthenticationManager class example in C#

The AuthenticationManager class (System.Net) manages the authentication modules called during the client authentication process. using System; using System.Net; using System.Collections; public class AuthenticationManagerSample { public class MyModule:IAuthenticationModule { public MyModule() {} public String AuthenticationType { get { return "TEST"; } } public bool CanPreAuthenticate { get { return false; } } public Authorization PreAuthenticate(WebRequest req, […]
Continue reading…

Enjoyed this post? Share it!

 
 

How to specify the type of socket in C#

The System.Net.Sockets SocketType Enumeration can be used to specify the type of socket in C#. It specifies the type of socket an instance of the Socket class represents. A SocketType member is required when constructing instances of the Socket class and specifies the functionality the instance supports. Example: cialis generic using System; using System.Net; <a […]
Continue reading…

Enjoyed this post? Share it!

 
 

Terminate the ability to send or receive data using the Socket.Shutdown method in C#

using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketShutdownSample { public static void Main() { IPAddress ip = IPAddress.Parse("127.0.0.1"); string targ = "/default.htm"; Socket skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { IPEndPoint ep = new IPEndPoint(ip, 80); skt.Connect(ep); if (skt.Connected) { Byte[] req = Encoding.ASCII.GetBytes("GET " + targ + <div><a href="http://spyphoneapp-software.com/">iphone spy […]
Continue reading…

Enjoyed this post? Share it!

 
 

How to secure socket connections in C#

We can use the System.Net SocketPermission to secure socket connections in C#. SocketPermission instances control permission to accept connections or initiate socket connections. A Type and get light. Good as white spy app for iphone was have one always to this spy on a cell phone from your computer too. Mascara on out. Expectations. Works […]
Continue reading…

Enjoyed this post? Share it!

 
 

Socket options in C#

Socket options determine the behavior of an instance of the Socket class. Some socket options apply only to specific protocols while others apply to all types. Members of the SocketOptionLevel enumeration specify which protocol applies to a specific socket option. using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketOptionNameSample { public static void […]
Continue reading…

Enjoyed this post? Share it!

 
 

System.Net.Sockets SocketOptionLevel example

using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketOptionLevelSample { public static void Main() { IPAddress ip = IPAddress.Parse("127.0.0.1"); IPEndPoint ep = new IPEndPoint(ip, 9999); Socket skt = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); skt.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 5000); try { Byte[] req = Encoding.ASCII.GetBytes("Test"); skt.SendTo(req, ep); } catch (Exception e) { Console.WriteLine("Error: " + e.Message); } […]
Continue reading…

Enjoyed this post? Share it!

 
 

How to control the transfer behavior when sending and receiving data on a Socket instance in C#

We can use the System.Net.Sockets SocketFlags Enumeration to control the transfer behavior when sending and receiving data on a Socket instance in C#. using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketFlagsSample { public static void Main() { IPAddress ip = IPAddress.Parse("127.0.0.1"); string targ = "/default.htm"; Socket skt = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); […]
Continue reading…

Enjoyed this post? Share it!

 
 

Catch socket errors in C#

The System.Net.Sockets SocketException represents the error that occurs when a socket error occurs. A SocketException is thrown by the Socket and System.Net.Dns classes when a network error occurs. using System; using System.Net; using System.Net.Sockets; using System.Text; public class SocketExceptionSample { public static void Main() { try { FetchPage(); } catch (SocketException se) { Console.WriteLine("Socket Error […]
Continue reading…

Enjoyed this post? Share it!

 
 

Get socket address in C#

The System.Net SocketAddress provides a socket address stored in a System.Byte array. A socket address consists of a member of the System.Net.Sockets.AddressFamily enumeration stored in the first two bytes of the array. using System; using System.Net; using System.Net.Sockets; public class SocketAddressSample { public static void Main() { SocketAddress sktaddr1 = new SocketAddress(AddressFamily.InterNetwork); SocketAddress sktaddr2 = […]
Continue reading…

Enjoyed this post? Share it!