| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using LitJson;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Networking;
- using UnityEngine.SceneManagement;
- /// <summary>
- /// 发送服务器
- /// </summary>
- public class HttpWebRequsetManger
- {
- public static void SendHttpData(string heraddata,string url,string requestJson, Action<string> callback)
- {
- try
- {
- byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(requestJson);
- HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url));
- webRequest.Method = "POST";
- webRequest.ContentType = "application/json";
- webRequest.ContentLength = byteArray.Length;
- webRequest.Headers.Add("x_auth_token", heraddata);
- webRequest.Timeout = 5000;
- Stream newStream = webRequest.GetRequestStream();
- newStream.Write(byteArray, 0, byteArray.Length);
- newStream.Close();
- HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
- StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- //检验状态码,如果成功接收数据
- int code = response.GetHashCode();
- //Debug.Log("code: " + code);
- string str = sr.ReadToEnd();
- //Debug.Log("str: " + str);
- //Log.Debug("HttpWebRequsetManger "+ str);
- webRequest.Abort();
- response.Close();
- sr.Close();
- callback.Invoke(str);
- }
- catch (Exception e)
- {
- e.ToString();
- //Log.Error("HttpWebRequsetManger Exception "+ e.ToString());
- Debug.Log("e: " + e.ToString());
- }
- }
- }
|