-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUniRequest.cs
127 lines (116 loc) · 3.98 KB
/
UniRequest.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
126
127
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Net;
using System.Threading;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using System.IO;
namespace Xupt_lib
{
public class UniRequest
{
public delegate void HandleResult(string result);
private HandleResult handle;
HttpWebRequest request;
HttpWebResponse response;
Dictionary<string, object> Params = new Dictionary<string, object>();
byte[] POSTBin;
string ResponseContent;
/// <summary>
/// 构造函数,获取URL、方法
/// </summary>
/// <param name="url">请求路径</param>
/// <param name="method">请求方法POST或GET</param>
public UniRequest(string url, string method = "GET")
{
try
{
request = (HttpWebRequest)HttpWebRequest.Create(url);
}
catch (Exception e)
{
Debug.WriteLine("Request Created Failed!" + e.Message);
return;
}
request.UserAgent = "XiyouLibrary_Windows_Phone_Client";
switch (method)
{
case "POST":
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
break;
case "GET": request.Method = "GET"; break;
default: request.Method = "GET"; break;
}
}
/// <summary>
/// 添加请求参数
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
public void AddParams(string key, object value)
{
if (request.Method == "GET")
{
Debug.WriteLine("Method Error!");
return;
}
Params.Add(key, value);
}
/// <summary>
/// 开始请求
/// </summary>
/// <param name="handle">请求结果</param>
public void StartRequest(HandleResult handle)
{
this.handle = handle;
switch (request.Method)
{
default:
case "GET": request.BeginGetResponse(new AsyncCallback(GetResponse), request); break;
case "POST": request.BeginGetRequestStream(new AsyncCallback(UploadParam), request); break;
}
}
void UploadParam(IAsyncResult result)
{
HttpWebRequest request = (HttpWebRequest)result.AsyncState;
using (Stream stream = request.EndGetRequestStream(result))
{
string tmp = "";
for (int i = 0; i < Params.Count; i++)
{
tmp += string.Format("{0}={1}", Params.ElementAtOrDefault(i).Key, Params.ElementAtOrDefault(i).Value);
if (i + 1 != Params.Count)
{
tmp += "&";
}
}
POSTBin = Encoding.UTF8.GetBytes(tmp);
stream.Write(POSTBin, 0, POSTBin.Length);
}
request.BeginGetResponse(new AsyncCallback(GetResponse), request);
}
void GetResponse(IAsyncResult result)
{
try
{
request = (HttpWebRequest)result.AsyncState;
response = (HttpWebResponse)request.EndGetResponse(result);
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
ResponseContent = reader.ReadToEnd();
}
}
catch (Exception e)
{
ResponseContent = "Request Error!";
Debug.WriteLine(e.InnerException.ToString());
}
Deployment.Current.Dispatcher.BeginInvoke(() => { handle(ResponseContent); });
}
}
}