سلام 😉 در این پست آموزش نمایش پیغام های خطا با try و catch و exception های شبکه (network) در c#.net را آماده کردم. انشاالله که این آموزش برای شما مفید باشد.
کد نمایش پیغام های خطا با try و catch و exception های شبکه (network) در c#.net :
using System;
using System.Net;
using System.IO;
class MainClass {
public static void Main() {
int ch;
try {
HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.java2s.com");
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
Stream istrm = resp.GetResponseStream();
for(int i=1; ; i++) {
ch = istrm.ReadByte();
if(ch == -1)
break;
Console.Write((char) ch);
}
resp.Close();
} catch(WebException exc) {
Console.WriteLine("Network Error: " + exc.Message +
"\nStatus code: " + exc.Status);
} catch(ProtocolViolationException exc) {
using System.Net;
using System.IO;
class MainClass {
public static void Main() {
int ch;
try {
HttpWebRequest req = (HttpWebRequest) WebRequest.Create("http://www.java2s.com");
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
Stream istrm = resp.GetResponseStream();
for(int i=1; ; i++) {
ch = istrm.ReadByte();
if(ch == -1)
break;
Console.Write((char) ch);
}
resp.Close();
} catch(WebException exc) {
Console.WriteLine("Network Error: " + exc.Message +
"\nStatus code: " + exc.Status);
} catch(ProtocolViolationException exc) {