-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
162 lines (156 loc) · 5.32 KB
/
Form1.cs
File metadata and controls
162 lines (156 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
/// <summary>
/// Code written by Shahid Riaz Bhatti on January 25, 2021
/// This code was written to clear the concept of sync vs async programming model.
/// Post is also written against it and its URL is http://argumentexception.com/2021/01/18/asynchronous-in-c/
/// </summary>
namespace Async_Practical
{
public partial class Form1 : Form
{
#region Global Variable
List<string> listofURLS = new List<string>();
#endregion
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Accept a URL and create a list and append some keywords in URL
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private List<string> PopulateTargetURL(string url)
{
listofURLS.Add(url + "/index");
listofURLS.Add(url + "/images");
listofURLS.Add(url + "/download");
listofURLS.Add(url + "/2006");
listofURLS.Add(url + "/news");
listofURLS.Add(url + "/crack");
listofURLS.Add(url + "/serial");
listofURLS.Add(url + "/wp-login.php");
return listofURLS;
}
#region Sync Call
/// <summary>
/// This method iterates through the Listof Urls and hit those urls and display
/// response and their status code inside the list view
/// </summary>
private void CheckUrls()
{
foreach (var item in listofURLS)
{
if (!string.IsNullOrEmpty(item))
{
lstScaningResult.Items.Add(new ListViewItem(new string[]
{
item,
Convert.ToString(HitURL(item))
}));
}
}
}
/// <summary>
/// Method which hit the URL and send back the status code
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private HttpStatusCode HitURL(string path)
{
HttpStatusCode myHttpWebResponse = new HttpStatusCode();
Uri myUri = new Uri(path);
WebRequest request = WebRequest.Create(myUri);
try
{
myHttpWebResponse = ((HttpWebResponse)request.GetResponse()).StatusCode;
}
catch (Exception ex)
{
if (ex.Message.Contains("404"))
{
return HttpStatusCode.NotFound;
}
}
return myHttpWebResponse;
}
#endregion
/// <summary>
/// Event for sync method
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button1_Click(object sender, EventArgs e)
{
lstScaningResult.Items.Clear();
PopulateTargetURL(txtURL.Text);
CheckUrls();
}
/// <summary>
/// event for async button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button2_Click(object sender, EventArgs e)
{
PopulateTargetURL(txtURL.Text);
CheckUrlsAsync();
}
/// <summary>
/// Iterates through the URLS in async manner and wait for the result and then display the result in list view
/// </summary>
private async void CheckUrlsAsync()
{
lstScaningResult.Items.Clear();
foreach (var item in listofURLS)
{
if (!string.IsNullOrEmpty(item))
{
HttpStatusCode task = await Task.Run(()=>HitURLAsync(item));
lstScaningResult.Items.Add(new ListViewItem(new string[]
{
item,
Convert.ToString(task)
}));
}
}
}
/// <summary>
/// Hit the URL in asycn manner
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private async Task<HttpStatusCode> HitURLAsync(string path)
{
return await Task.Run(() => {
HttpStatusCode myHttpWebResponse = new HttpStatusCode();
Uri myUri = new Uri(path);
WebRequest request = WebRequest.Create(myUri);
try
{
myHttpWebResponse = ((HttpWebResponse)request.GetResponse()).StatusCode;
}
catch (Exception ex)
{
if (ex.Message.Contains("404"))
{
return HttpStatusCode.NotFound;
}
}
return myHttpWebResponse;
});
}
}
}