-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHttpInvoke.cs
94 lines (82 loc) · 3.67 KB
/
HttpInvoke.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
//
// C# (.Net Framework)
// IncominHttpServer
// https://github.com/dkxce/IncominHttpServer
// en,ru,1251,utf-8
//
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using static IncominHttpServer.ThreadedHttpServer;
namespace IncominHttpServer
{
public class HttpInvoke
{
public ushort InvokeNumber = 0;
/// <summary>
/// Process Custom Requests
/// </summary>
/// <param name="Request"></param>
/// <returns>Process Next Logic</returns>
public bool GetClientRequest(ClientRequest Request)
{
if (InvokeNumber == 1) return Invoke_1(Request);
return true;
}
/// <summary>
/// Invoke 1
/// </summary>
/// <param name="Request"></param>
/// <returns>Process Next Logic</returns>
public bool Invoke_1(ClientRequest Request)
{
bool res = true;
const string url = "http://localhost:8800/api/v1/pay/buy/";
if (!string.IsNullOrEmpty(Request.Authorization) && Request.Authorization.StartsWith("Bearer") && Request.Headers.ContainsKey("Provider"))
{
res = false;
bool wait = false;
Thread thr = new Thread(new ThreadStart(() => {
HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(url);
wr.Headers.Add("Authorization", Request.Authorization);
wr.Headers.Add("Provider", Request.Headers["Provider"]);
wr.ContentType = Request.Headers["Content-Type"];
wr.Method = Request.Method.ToUpper().Trim();
wr.ContentLength = Request.BodyData.Length;
try
{
using (Stream reqstr = wr.GetRequestStream())
reqstr.Write(Request.BodyData, 0, Request.BodyData.Length);
HttpWebResponse wres = (HttpWebResponse)wr.GetResponse();
string response = "";
Encoding enc = Encoding.UTF8;
string tenc = wres.ContentEncoding;
if (!string.IsNullOrEmpty(tenc))
{
tenc = tenc.Replace("charset=", "");
if (!string.IsNullOrEmpty(tenc))
{
try { enc = Encoding.GetEncoding(tenc); } catch { };
};
};
using (StreamReader streamReader = new StreamReader(wres.GetResponseStream(), enc)) response = streamReader.ReadToEnd();
string dump = $"HttpWebResponse (Invoke_1) from {url}\r\n{DateTime.Now}\r\n{url}\r\n\r\nHTTP/{wres.ProtocolVersion} {(int)wres.StatusCode} {wres.StatusCode.ToString()}\r\n";
foreach (string key in wres.Headers.AllKeys) dump += $"{key}: {wres.Headers[key]} \r\n";
dump += "\r\n" + response;
ApplicationLog.WriteInvoke(dump);
wres.Close();
}
catch (Exception ex) {
ApplicationLog.WriteLn($"Error: {ex}");
ApplicationLog.WriteInvoke($"HttpWebResponse (Invoke_1) from {url}\r\nError: {ex}");
};
}));
thr.Start();
if(wait) while(thr.IsAlive) Thread.Sleep(100);
};
return res;
}
}
}