Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
ygopro2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Xu Chenxi
ygopro2
Commits
47e331f0
Commit
47e331f0
authored
May 14, 2019
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use HttpWebRequest
parent
d9bc99cc
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
28 deletions
+35
-28
Assets/SibylSystem/selectServer/MyCardHelper.cs
Assets/SibylSystem/selectServer/MyCardHelper.cs
+35
-28
No files found.
Assets/SibylSystem/selectServer/MyCardHelper.cs
View file @
47e331f0
using
UnityEngine
;
using
UnityEngine
;
using
UnityEngine.Networking
;
using
System
;
using
System
;
using
System.Text
;
using
System.Text
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.IO
;
using
System.Net
;
public
class
JSONObject
{
public
class
JSONObject
{
public
string
Stringify
()
public
string
Stringify
()
...
@@ -52,28 +52,34 @@ public class MatchObject : JSONObject {
...
@@ -52,28 +52,34 @@ public class MatchObject : JSONObject {
public
class
MyCardHelper
{
public
class
MyCardHelper
{
string
username
=
null
;
string
username
=
null
;
int
userid
=
-
1
;
int
userid
=
-
1
;
public
bool
login
(
string
name
,
string
password
,
out
string
fail_reason
)
{
public
bool
login
(
string
name
,
string
password
,
out
string
fail_reason
)
{
try
{
try
{
LoginRequest
data
=
new
LoginRequest
(
name
,
password
);
LoginRequest
data
=
new
LoginRequest
(
name
,
password
);
string
data_str
=
data
.
Stringify
();
string
data_str
=
data
.
Stringify
();
UnityWebRequest
www
=
UnityWebRequest
.
Post
(
"https://api.moecube.com/accounts/signin"
,
data_str
);
HttpWebRequest
request
=
(
HttpWebRequest
)
WebRequest
.
Create
(
"https://api.moecube.com/accounts/signin"
);
www
.
SetRequestHeader
(
"Content-Type"
,
"application/json"
);
request
.
Method
=
"POST"
;
www
.
SendWebRequest
();
request
.
ContentType
=
"application/json"
;
while
(!
www
.
isDone
)
{
request
.
ContentLength
=
Encoding
.
UTF8
.
GetByteCount
(
data_str
);
if
(
www
.
isNetworkError
||
www
.
isHttpError
)
Stream
request_stream
=
request
.
GetRequestStream
();
{
StreamWriter
stream_writer
=
new
StreamWriter
(
request_stream
,
Encoding
.
UTF8
);
fail_reason
=
www
.
error
;
stream_writer
.
Write
(
data_str
);
return
false
;
stream_writer
.
Close
();
}
}
HttpWebResponse
response
=
(
HttpWebResponse
)
request
.
GetResponse
();
if
(
www
.
responseCode
>=
400
)
if
(
response
.
StatusCode
>=
400
)
{
{
fail_reason
=
"Login failed"
;
fail_reason
=
response
.
StatusDescription
;
return
false
;
return
false
;
}
}
else
else
{
{
string
result
=
www
.
downloadHandler
.
text
;
Stream
response_stream
=
response
.
GetResponseStream
();
StreamReader
stream_reader
=
new
StreamReader
(
response_stream
,
Encoding
.
UTF8
);
string
result
=
stream_reader
.
ReadToEnd
();
stream_reader
.
Close
();
response_stream
.
Close
();
LoginObject
result_object
=
JsonUtility
.
FromJson
<
LoginObject
>(
result
);
LoginObject
result_object
=
JsonUtility
.
FromJson
<
LoginObject
>(
result
);
username
=
result_object
.
user
.
username
;
username
=
result_object
.
user
.
username
;
userid
=
result_object
.
user
.
id
;
userid
=
result_object
.
user
.
id
;
...
@@ -93,24 +99,25 @@ public class MyCardHelper {
...
@@ -93,24 +99,25 @@ public class MyCardHelper {
}
}
try
{
try
{
string
auth_str
=
Convert
.
ToBase64String
(
Encoding
.
UTF8
.
GetBytes
(
username
+
":"
+
userid
));
string
auth_str
=
Convert
.
ToBase64String
(
Encoding
.
UTF8
.
GetBytes
(
username
+
":"
+
userid
));
UnityWebRequest
www
=
UnityWebRequest
.
Post
(
"https://api.mycard.moe/ygopro/match?locale=zh-CN&arena="
+
match_type
,
new
WWWForm
());
HttpWebRequest
request
=
(
HttpWebRequest
)
WebRequest
.
Create
(
"https://api.mycard.moe/ygopro/match?locale=zh-CN&arena="
+
match_type
);
www
.
SetRequestHeader
(
"Authorization"
,
"Basic "
+
auth_str
);
request
.
Method
=
"POST"
;
www
.
SendWebRequest
();
request
.
ContentType
=
"application/x-www-form-urlencoded"
;
while
(!
www
.
isDone
)
{
request
.
Headers
.
Add
(
"Authorization"
,
"Basic "
+
auth_str
);
if
(
www
.
isNetworkError
||
www
.
isHttpError
)
{
HttpWebResponse
response
=
(
HttpWebResponse
)
request
.
GetResponse
();
fail_reason
=
www
.
error
;
return
null
;
if
(
response
.
StatusCode
>=
400
)
}
}
if
(
www
.
responseCode
>=
400
)
{
{
fail_reason
=
"Match failed"
;
fail_reason
=
response
.
StatusDescription
;
return
null
;
return
false
;
}
}
else
else
{
{
string
result
=
www
.
downloadHandler
.
text
;
Stream
response_stream
=
response
.
GetResponseStream
();
StreamReader
stream_reader
=
new
StreamReader
(
response_stream
,
Encoding
.
UTF8
);
string
result
=
stream_reader
.
ReadToEnd
();
stream_reader
.
Close
();
response_stream
.
Close
();
MatchObject
result_object
=
JsonUtility
.
FromJson
<
MatchObject
>(
result
);
MatchObject
result_object
=
JsonUtility
.
FromJson
<
MatchObject
>(
result
);
ret
=
result_object
.
password
;
ret
=
result_object
.
password
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment