This repository has been archived by the owner on Nov 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
AsyncWebRequest.cs
125 lines (110 loc) · 3.72 KB
/
AsyncWebRequest.cs
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
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Threading;
namespace Oxide
{
/// <summary>
/// An asynchronous web request
/// </summary>
public class AsyncWebRequest
{
private WebRequest request;
private string url;
private string postdata;
private bool ispost;
private Thread thread;
public bool Complete { get; private set; }
public int ResponseCode { get; private set; }
public string Response { get; private set; }
private bool handled;
public event Action<AsyncWebRequest> OnResponse;
public AsyncWebRequest(string url)
{
this.url = url;
this.postdata = null;
this.ispost = false;
thread = new Thread(Worker);
thread.Start();
//Main.Log("Worker thread started...");
}
public AsyncWebRequest(string url, string postdata)
{
this.url = url;
this.postdata = postdata;
this.ispost = true;
thread = new Thread(Worker);
thread.Start();
}
private void Worker()
{
try
{
//Main.Log("Creating request...");
request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
//30 Second timeout instead of 2 minutes before.
request.Timeout = 30000;
//Main.Log("Waiting for response...");
if (this.ispost == true && this.postdata != null)
{
request.Method = "POST";
byte[] bytePostData = Encoding.UTF8.GetBytes(postdata);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytePostData.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(bytePostData, 0, bytePostData.Length);
postStream.Close();
}
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
if (response == null)
{
// Uhh panic what do we do
Logger.Error("AsyncWebRequest panic: Response is null!");
return;
}
//Main.Log("Reading response stream...");
Stream strm = response.GetResponseStream();
StreamReader rdr = new StreamReader(strm);
Response = rdr.ReadToEnd();
ResponseCode = (int)response.StatusCode;
rdr.Close();
strm.Close();
response.Close();
//Main.Log("Web request complete.");
}
catch (WebException webex)
{
var response = webex.Response as HttpWebResponse;
Response = webex.Message;
ResponseCode = (int)response.StatusCode;
}
catch (Exception ex)
{
Response = ex.ToString();
ResponseCode = -1;
Logger.Error(ex.ToString());
}
finally
{
Complete = true;
}
}
/// <summary>
/// Updates this request
/// </summary>
public void Update()
{
if (Complete && !handled)
{
handled = true;
if (OnResponse != null)
{
//Main.Log("Firing web request callback...");
OnResponse(this);
}
}
}
}
}