少年修仙传客户端代码仓库
client_Wu Xijin
2018-09-04 d9e6e5aac14261fad5bd18053b600bd513b45509
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine;
 
public class HttpRequest : SingletonMonobehaviour<HttpRequest>
{
    public const string defaultHttpContentType = "application/x-www-form-urlencoded";
    public const string jsonHttpContentType = "application/json ; charset=utf-8";
 
    public void RequestWWW(string _url, int _retry = 3, Action<bool, string> _result = null)
    {
        StartCoroutine(Co_RequestWWW(_url, null, _retry, _result));
    }
 
    public void RequestWWW(string _url, IDictionary<string, string> _parameters, int _retry = 3, Action<bool, string> _result = null)
    {
        StartCoroutine(Co_RequestWWW(_url, _parameters, _retry, _result));
    }
 
    IEnumerator Co_RequestWWW(string _url, IDictionary<string, string> _parameters, int _retry = 3, Action<bool, string> _result = null)
    {
        if (_url == null || _url.Length == 0)
        {
            DebugEx.LogError("PHPDataComm post 参数有错");
            if (_result != null)
            {
                _result(false, string.Empty);
                _result = null;
            }
            yield break;
        }
 
        int i = 0;
        bool isSuccess = false;
 
        byte[] data = null;
        if (_parameters != null)
        {
            data = Encoding.UTF8.GetBytes(HashtablaToString(_parameters));
        }
 
        var PostData = data == null ? new WWW(_url) : new WWW(_url, data);
 
        while (!PostData.isDone)
        {
            yield return null;
        }
 
        if (PostData.error != null)
        {
            Debug.LogErrorFormat("WWW 数据通信,请求数据失败:{0},已经尝试,{1},次", PostData.error, i);
        }
        else
        {
            if (!string.IsNullOrEmpty(PostData.text))
            {
                DebugEx.LogFormat("WWW 数据通信,请求数据成功:{0}", PostData.text);
                isSuccess = true;
                if (_result != null)
                {
                    _result(true, PostData.text);
                    _result = null;
                }
            }
        }
 
        if (!isSuccess)
        {
            if (_result != null)
            {
                _result(false, string.Empty);
                _result = null;
            }
        }
    }
 
    public void RequestHttpPost(string _url, IDictionary<string, string> _parameters, string _contentType, int _retry = 3, Action<bool, string> _result = null)
    {
        var content = HashtablaToString(_parameters);
        HttpBehaviour.Create(_url, "POST", content, _contentType, _retry, _result);
    }
 
    public void RequestHttpPost(string _url, string _content, string _contentType, int _retry = 3, Action<bool, string> _result = null)
    {
        HttpBehaviour.Create(_url, "POST", _content, _contentType, _retry, _result);
    }
 
    public void RequestHttpGet(string _url, string _contentType, int _retry = 3, Action<bool, string> _result = null)
    {
        HttpBehaviour.Create(_url, "GET", "", _contentType, _retry, _result);
    }
 
    static StringBuilder buffer = new StringBuilder();
    public static string HashtablaToString(IDictionary<string, string> parameters)
    {
        buffer.Remove(0, buffer.Length);
        int i = 0;
        foreach (KeyValuePair<string, string> item in parameters)
        {
            if (i > 0)
            {
                buffer.AppendFormat("&{0}={1}", item.Key, item.Value);
            }
            else
            {
                buffer.AppendFormat("{0}={1}", item.Key, item.Value);
            }
            i++;
        }
        string result = buffer.ToString();
        return result;
    }
 
}