Networking in C#

PHPz
Release: 2023-09-07 18:29:09
forward
931 people have browsed it

C# 中的网络

.NET Framework features a layered, extensible, and managed implementation of web services. You can easily integrate them into your application. Use System.Net; namespace.

Let us see how to access the Uri class: In C#, it provides an object representation of a Uniform Resource Identifier (URI) -

Uri uri = new Uri("http://www.example.com/");
WebRequest w = WebRequest.Create(uri);
Copy after login

Now let us see the System.Net class. This is used to encrypt the connection using Secure Sockets Layer (SSL). If the URI begins with "https:", SSL is used; if the URI begins with "http:", an unencrypted connection is used.

The following are examples. For SSL using FTP, set the EnableSsl property to true before calling the GetResponse() method.

String uri = "https://www.example.com/";
WebRequest w = WebRequest.Create(uri);

String uriServer = "ftp://ftp.example.com/new.txt"
FtpWebRequest r = (FtpWebRequest)WebRequest.Create(uriServer);
r.EnableSsl = true;
r.Method = WebRequestMethods.Ftp.DeleteFile;
Copy after login

The following example shows the use of the System.Net namespace and the use of the Dns.GetHostEntry, Dns.GetHostName method, and IPHostEntry properties AddressList -

Example

using System;
using System.Net;

class Program {
   static void Main() {

      String hostName = string.Empty;
      hostName = Dns.GetHostName();
      Console.WriteLine("Hostname: "+hostName);
      IPHostEntry myIP = Dns.GetHostEntry(hostName);

      IPAddress[] address = myIP.AddressList;

      for (int i = 0; i < address.Length; i++) {
         Console.WriteLine("IP Address {1} : ",address[i].ToString());
      }
      Console.ReadLine();
   }
}
Copy after login

The above is the detailed content of Networking in C#. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template