|
35
|
1 |
using System; |
|
|
2 |
using System.Net; |
|
|
3 |
using System.Windows; |
|
|
4 |
using System.Windows.Controls; |
|
|
5 |
using System.Windows.Documents; |
|
|
6 |
using System.Windows.Ink; |
|
|
7 |
using System.Windows.Input; |
|
|
8 |
using System.Windows.Media; |
|
|
9 |
using System.Windows.Media.Animation; |
|
|
10 |
using System.Windows.Shapes; |
|
|
11 |
using System.Collections.Generic; |
|
|
12 |
using System.IO; |
|
|
13 |
using System.Threading; |
|
|
14 |
namespace Iri.Modernisation.Data.Models |
|
|
15 |
{ |
|
|
16 |
public class HttpSender |
|
|
17 |
{ |
|
|
18 |
private HttpWebRequest Request { get; set; } |
|
|
19 |
public Dictionary<string, string> PostValues { get; private set; } |
|
|
20 |
public String Response { get; private set; } |
|
|
21 |
|
|
|
22 |
public event HttpResponseCompleteEventHandler ResponseComplete; |
|
|
23 |
private void OnResponseComplete(HttpResponseCompleteEventArgs e) |
|
|
24 |
{ |
|
|
25 |
if (this.ResponseComplete != null) |
|
|
26 |
{ |
|
38
|
27 |
ThreadPool.QueueUserWorkItem(new WaitCallback((Object o)=>this.ResponseComplete(e))); |
|
|
28 |
// this.ResponseComplete(e); |
|
35
|
29 |
} |
|
|
30 |
} |
|
|
31 |
private void OnResponseComplete() |
|
|
32 |
{ |
|
|
33 |
if (this.ResponseComplete != null) |
|
|
34 |
{ |
|
38
|
35 |
ThreadPool.QueueUserWorkItem(new WaitCallback((Object o) => new HttpResponseCompleteEventArgs(Response))); |
|
35
|
36 |
this.ResponseComplete(new HttpResponseCompleteEventArgs(Response)); |
|
|
37 |
} |
|
|
38 |
} |
|
|
39 |
public HttpSender(Uri requestUri, string method, params KeyValuePair<string, string>[] postValues) |
|
|
40 |
{ |
|
|
41 |
this.Request = (HttpWebRequest)WebRequest.Create(requestUri); |
|
|
42 |
this.Request.ContentType = "application/x-www-form-urlencoded"; |
|
|
43 |
this.Request.Method = method; |
|
|
44 |
this.PostValues = new Dictionary<string, string>(); |
|
|
45 |
if (postValues != null && postValues.Length > 0) |
|
|
46 |
{ |
|
|
47 |
foreach (var item in postValues) |
|
|
48 |
{ |
|
|
49 |
this.PostValues.Add(item.Key, item.Value); |
|
|
50 |
} |
|
|
51 |
} |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
public void Execute() |
|
|
55 |
{ |
|
|
56 |
this.Request.BeginGetRequestStream(new AsyncCallback(BeginRequest), this); |
|
|
57 |
|
|
|
58 |
} |
|
|
59 |
|
|
|
60 |
private void BeginRequest(IAsyncResult ar) |
|
|
61 |
{ |
|
|
62 |
HttpSender helper = ar.AsyncState as HttpSender; |
|
|
63 |
if (helper != null) |
|
|
64 |
{ |
|
|
65 |
if (helper.PostValues.Count > 0) |
|
|
66 |
{ |
|
|
67 |
using (StreamWriter writer = new StreamWriter(helper.Request.EndGetRequestStream(ar))) |
|
|
68 |
{ |
|
|
69 |
foreach (var item in helper.PostValues) |
|
|
70 |
{ |
|
|
71 |
writer.Write("{0}={1}&", item.Key,item.Value); |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
} |
|
|
75 |
} |
|
|
76 |
helper.Request.BeginGetResponse(new AsyncCallback(BeginResponse), helper); |
|
|
77 |
} |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
private void BeginResponse(IAsyncResult ar) |
|
|
81 |
{ |
|
|
82 |
HttpSender helper = ar.AsyncState as HttpSender; |
|
|
83 |
if (helper != null) |
|
|
84 |
{ |
|
|
85 |
HttpWebResponse response = (HttpWebResponse)helper.Request.EndGetResponse(ar); |
|
|
86 |
if (response != null) |
|
|
87 |
{ |
|
|
88 |
Stream stream = response.GetResponseStream(); |
|
|
89 |
if (stream != null) |
|
|
90 |
{ |
|
|
91 |
using (StreamReader reader = new StreamReader(stream)) |
|
|
92 |
{ |
|
|
93 |
helper.Response = reader.ReadToEnd(); |
|
|
94 |
} |
|
|
95 |
} |
|
|
96 |
} |
|
|
97 |
} |
|
|
98 |
this.OnResponseComplete(); |
|
|
99 |
} |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
public delegate void HttpResponseCompleteEventHandler(HttpResponseCompleteEventArgs e); |
|
|
103 |
public class HttpResponseCompleteEventArgs : EventArgs |
|
|
104 |
{ |
|
|
105 |
public string Response { get; set; } |
|
|
106 |
|
|
|
107 |
public HttpResponseCompleteEventArgs(string response) |
|
|
108 |
{ |
|
|
109 |
this.Response = response; |
|
|
110 |
} |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
|
|
|
114 |
/* |
|
|
115 |
using Of the class |
|
|
116 |
private void ProcessCommand(short cmd, string msg) |
|
|
117 |
{ |
|
|
118 |
App app = App.Current as App; |
|
|
119 |
HttpHelper helper = new HttpHelper(app.ServerUri, "POST", |
|
|
120 |
new KeyValuePair<string, string>("authKey", app.AuthKey), |
|
|
121 |
new KeyValuePair<string, string>("cmd", cmd.ToString()), |
|
|
122 |
new KeyValuePair<string, string>("msg", msg)); |
|
|
123 |
helper.ResponseComplete += new HttpResponseCompleteEventHandler(this.CommandComplete); |
|
|
124 |
helper.Execute(); |
|
|
125 |
|
|
|
126 |
} |
|
|
127 |
|
|
|
128 |
private void CommandComplete(HttpResponseCompleteEventArgs e) |
|
|
129 |
{ |
|
|
130 |
txtAlert.Text = e.Response; |
|
|
131 |
} |
|
|
132 |
*/ |
|
|
133 |
|
|
|
134 |
} |