-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHelper.cs
124 lines (122 loc) · 4.28 KB
/
Helper.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
using System;
using System.Collections.Specialized;
using System.Text;
using System.Threading;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace Helper
{
public class EmbeddedBrowserRequest
{
private Uri requestUri;
private byte[] postData;
private string additionalHeaders;
public Uri ResponseUri { get; private set; }
public Uri RedirectUri { get; set; }
public string DocumentText { get; private set; }
public EmbeddedBrowserRequest()
{
}
private void Navigating(object sender, NavigatingCancelEventArgs e)
{
var form = (sender as WebBrowser).Parent as Window;
//Console.WriteLine("Navigating " + e.Uri);
var b = new UriBuilder(e.Uri);
b.Query = null;
if (this.RedirectUri == b.Uri)
{
this.ResponseUri = e.Uri;
this.DocumentText = (sender as WebBrowser).Document as string;
form.DialogResult = true;
form.Close();
e.Cancel = true;
}
}
private void Navigated(object sender, NavigationEventArgs e)
{
var form = (sender as WebBrowser).Parent as Window;
//Console.WriteLine("Navigated " + e.Uri);
var b = new UriBuilder(e.Uri);
b.Query = null;
if (this.RedirectUri == b.Uri)
{
this.ResponseUri = e.Uri;
this.DocumentText = (sender as WebBrowser).Document as string;
form.DialogResult = true;
form.Close();
}
}
private void LoadCompleted(object sender, NavigationEventArgs e)
{
//Console.WriteLine("LoadCompleted " + e.Uri);
}
private void Run()
{
WebBrowser web;
var form = new Window()
{
Width = 800,
Height = 640,
Content = (web = new WebBrowser())
};
//form.Width = 800;
//form.Height = 640;
//var web = new WebBrowser();
//web.Size = form.ClientSize;
//web.Anchor = (AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom);
web.Navigating += Navigating;
web.Navigated += Navigated;
web.LoadCompleted += LoadCompleted;
if (postData != null)
{
web.Navigate(requestUri, null, postData, additionalHeaders);
}
else
{
web.Navigate(requestUri);
}
form.ShowDialog();
}
public Uri AuthorizationRequest(Uri authorizationRequest)
{
if (authorizationRequest.Query == null)
{
throw new ArgumentException("WebBrowserRequest", "authorizationRequest");
}
NameValueCollection qs = HttpUtility.ParseQueryString(authorizationRequest.Query);
var s = qs.Get("redirect_uri");
if (s == null)
{
throw new ArgumentException("WebBrowserRequest", "redirect_uri");
}
this.RedirectUri = new Uri(s);
return Get(authorizationRequest)
? this.ResponseUri
: null;
}
public bool Get(Uri requestUri)
{
this.requestUri = requestUri;
this.postData = null;
this.additionalHeaders = null;
var t = new Thread(this.Run);
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
return this.ResponseUri != null;
}
public bool Post(Uri requestUri, string data)
{
this.requestUri = requestUri;
this.postData = Encoding.UTF8.GetBytes(data);
this.additionalHeaders = "Content-Type: application/x-www-form-urlencoded";
var t = new Thread(this.Run);
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
return this.ResponseUri != null;
}
}
}