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;
|
}
|
|
}
|