添加HTTP 发送post请求方法
parent
d78ce3fe7c
commit
7af10f453c
|
@ -52,7 +52,7 @@ namespace GameLogic
|
|||
Reset();
|
||||
}
|
||||
/// <summary>
|
||||
/// 析构函数
|
||||
/// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
void OnDestroy()
|
||||
{
|
||||
|
@ -114,7 +114,6 @@ namespace GameLogic
|
|||
}
|
||||
yield break;
|
||||
}
|
||||
|
||||
UnityWebRequest request = UnityWebRequest.Get(url);
|
||||
request.certificateHandler = new AcceptAllCertificatesSignedWithASpecificPublicKey();
|
||||
|
||||
|
@ -150,16 +149,78 @@ namespace GameLogic
|
|||
|
||||
public void SendHttpPost_Json_Lua(string url, string data, LuaFunction callback, LuaFunction errorLuaFunc)
|
||||
{
|
||||
//Debug.LogError("data==="+data);
|
||||
var strs = data.Split(';');
|
||||
Hashtable table = new Hashtable();
|
||||
for (int i = 0; i < strs.Length; i = i + 2)
|
||||
{
|
||||
table.Add(strs[i], strs[i + 1]);
|
||||
string v1 = strs[i];
|
||||
string v3 = strs[i + 1];
|
||||
//long v2 = 0;
|
||||
//if (strs[i].Equals("total_amount") || strs[i].Equals("trade_time") || strs[i].Equals("valid_time"))
|
||||
//{
|
||||
// long.TryParse(v3, out v2);
|
||||
// Debug.LogError("v2=="+v2);
|
||||
// table.Add(v1, v2);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// table.Add(v1, strs[i + 1]);
|
||||
//}
|
||||
table.Add(v1, v3);
|
||||
|
||||
}
|
||||
var jsonData = MiniJSON.jsonEncode(table);
|
||||
StartCoroutine(HttpPost_Co(url, jsonData, callback, null, null, errorLuaFunc));
|
||||
Debug.LogError("url " + jsonData);
|
||||
// StartCoroutine(HttpPost_Co(url, jsonData, callback, null, null, errorLuaFunc));
|
||||
StartCoroutine(PostData(url, jsonData, callback, null, null, errorLuaFunc));
|
||||
}
|
||||
|
||||
public IEnumerator PostData(string url, string data, LuaFunction callback, Action<string> sharpFunc, Action errorAction, LuaFunction errorLuaFunc)
|
||||
{
|
||||
//UnityWebRequest _request;
|
||||
//byte[] databyte = Encoding.UTF8.GetBytes(data);
|
||||
//_request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
|
||||
//_request.uploadHandler = new UploadHandlerRaw(databyte);
|
||||
//_request.downloadHandler = new DownloadHandlerBuffer();
|
||||
//_request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
|
||||
//yield return _request.SendWebRequest();
|
||||
//Debug.Log(_request.responseCode);
|
||||
|
||||
//if (_request.isHttpError || _request.isNetworkError)
|
||||
//{
|
||||
// Debug.LogError(_request.error);
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
// Debug.Log(_request.downloadHandler.text);
|
||||
//if (callback != null)
|
||||
//{
|
||||
// callback.Call(_request.downloadHandler.text);
|
||||
//}
|
||||
|
||||
//}
|
||||
UnityWebRequest www = UnityWebRequest.Post(url, data);
|
||||
www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");
|
||||
byte[] bodyRaw = Encoding.UTF8.GetBytes(data);
|
||||
www.uploadHandler = new UploadHandlerRaw(bodyRaw);
|
||||
yield return www.SendWebRequest();
|
||||
Debug.LogError("111111111111");
|
||||
if (www.isNetworkError)
|
||||
{
|
||||
Debug.LogError(www.error);
|
||||
}
|
||||
Debug.LogError(www.downloadHandler.text);
|
||||
if (callback != null)
|
||||
{
|
||||
callback.Call(www.downloadHandler.text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void SendHttpPost_Raw_CSharp(string url, string data, Action<string> sharpFunc, Action errorAction)
|
||||
{
|
||||
StartCoroutine(HttpPost_Co(url, data, null, sharpFunc, errorAction, null));
|
||||
|
@ -198,6 +259,12 @@ namespace GameLogic
|
|||
Dictionary<string, string> header = new Dictionary<string, string>();
|
||||
header.Add("Content-Type", "application/json");
|
||||
header.Add("charset", "utf-8");
|
||||
Debug.LogError("data=="+data);
|
||||
Debug.LogError("utf8=="+Encoding.UTF8.GetBytes(data));
|
||||
byte[] b=Encoding.UTF8.GetBytes(data);
|
||||
for (int i=0;i<b.Length;i++){
|
||||
Debug.LogError(b[i]);
|
||||
}
|
||||
WWW postData = new WWW(url, Encoding.UTF8.GetBytes(data), header);
|
||||
while (!postData.isDone)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue