FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CefSharp/CefSharp.Example/WebRequestResourceHandler.cs at master · SkyNetStudio/CefSharp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SkyNetStudio
/
CefSharp
Public
forked from
cefsharp/CefSharp
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
CefSharp
/
CefSharp.Example
/
WebRequestResourceHandler.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
79 lines (65 loc) · 3.1 KB
Breadcrumbs
CefSharp
/
CefSharp.Example
/
WebRequestResourceHandler.cs
Copy path
File metadata and controls
79 lines (65 loc) · 3.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Copyright © 2016 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using
System
.
Collections
.
Specialized
;
using
System
.
IO
;
using
System
.
Net
;
using
System
.
Net
.
Mime
;
using
System
.
Threading
.
Tasks
;
namespace
CefSharp
.
Example
{
/// <summary>
/// A simple <see cref="ResourceHandler"/> that uses <see cref="WebRequest.Create(string)"/> to fulfill requests.
/// </summary>
/// <remarks>
/// This example doesn't cover all cases, for example POST requests, if you'd like to see the example
/// expanded then please subit a Pull Request.
/// </remarks>
public
class
WebRequestResourceHandler
:
ResourceHandler
{
public
override
CefReturnValue
ProcessRequestAsync
(
IRequest
request
,
ICallback
callback
)
{
//Spawn a Task and immediately return CefReturnValue.ContinueAsync
Task
.
Run
(
async
(
)
=>
{
using
(
callback
)
{
//Create a clone of the headers so we can modify it
var
headers
=
new
NameValueCollection
(
request
.
Headers
)
;
var
httpWebRequest
=
(
HttpWebRequest
)
WebRequest
.
Create
(
request
.
Url
)
;
httpWebRequest
.
UserAgent
=
headers
[
"User-Agent"
]
;
httpWebRequest
.
Accept
=
headers
[
"Accept"
]
;
httpWebRequest
.
Method
=
request
.
Method
;
httpWebRequest
.
Referer
=
request
.
ReferrerUrl
;
//These headers must be set via the appropriate properties.
headers
.
Remove
(
"User-Agent"
)
;
headers
.
Remove
(
"Accept"
)
;
httpWebRequest
.
Headers
.
Add
(
headers
)
;
//TODO: Deal with post data
var
postData
=
request
.
PostData
;
var
httpWebResponse
=
await
httpWebRequest
.
GetResponseAsync
(
)
as
HttpWebResponse
;
// Get the stream associated with the response.
var
receiveStream
=
httpWebResponse
.
GetResponseStream
(
)
;
var
contentType
=
new
ContentType
(
httpWebResponse
.
ContentType
)
;
var
mimeType
=
contentType
.
MediaType
;
var
charSet
=
contentType
.
CharSet
;
var
statusCode
=
httpWebResponse
.
StatusCode
;
var
memoryStream
=
new
MemoryStream
(
)
;
receiveStream
.
CopyTo
(
memoryStream
)
;
receiveStream
.
Dispose
(
)
;
httpWebResponse
.
Dispose
(
)
;
//Reset the stream position to 0 so the stream can be copied into the underlying unmanaged buffer
memoryStream
.
Position
=
0
;
ResponseLength
=
memoryStream
.
Length
;
MimeType
=
mimeType
;
Charset
=
charSet
??
"UTF-8"
;
StatusCode
=
(
int
)
statusCode
;
Stream
=
memoryStream
;
AutoDisposeStream
=
true
;
callback
.
Continue
(
)
;
}
}
)
;
return
CefReturnValue
.
ContinueAsync
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL