Unity C# 之 使用 HttpWebRequest 基础知识/HttpWebRequest 进行异步Post 网络访问/数据流形式获取数据(Task/async/await)的代码简单实现

avatar
作者
猴君
阅读量:4

Unity C# 之 使用 HttpWebRequest 基础知识/HttpWebRequest 进行异步Post 网络访问/数据流形式获取数据(Task/async/await)的代码简单实现

目录

Unity C# 之 使用 HttpWebRequest 基础知识/HttpWebRequest 进行异步Post 网络访问/数据流形式获取数据(Task/async/await)的代码简单实现

一、简单介绍

二、实现原理

三、注意事项

四、效果预览

五、关键代码

附录 : HttpWebRequest 的一些基础知识

1、HttpWebRequest 常用属性

2、HttpWebRequest  中的 ContentType

3、HttpWebRequest 一般使用示例

4、HttpWebRequest  json 文本 Post 请求示例

5、 HttpWebRequest   multipart/form-data 文件上传示例


一、简单介绍

Unity 在开发中,网络访问:

  • 可以使用 UnityWebRequest 访问,不过好似只能用协程的方式,并且访问只能在主线程中;
  • 所以这里使用 C# 中的 HttpWebRequest,进行网络访问,而且 HttpWebRequest,允许在子线程中访问,在一些情况下可以大大减少主线程的网络访问压力; 

这里使用 HttpWebRequest ,进行 Post 访问,并且Task 结合 async (await) 的进行异步访问,最后使用 Stream 流式的形式获取数据,在这里做一个简单的记录,以便后期使用的时候参考。

二、实现原理

1、HttpWebRequest 创建 post 的请求

2、GetRequestStreamAsync 发起 异步请求

3、GetResponseAsync 和 GetResponseStream 异步获取响应数据流

4、因为使用到异步,所以 async Task<string> 返回异步获取的流数据

三、注意事项

1、获取更新的流数据打印是,注意 每次更新下 buffer ,不然输出打印的结果可能不是预期的

    // 循环获取流式数据     while ((bytesRead = await reader.ReadAsync(buffer, 0, buffer.Length)) > 0)     {          sb.Append(buffer, 0, bytesRead);          Console.WriteLine(buffer);          buffer = new char[1024];      }

四、效果预览

五、关键代码

using System; using System.Net; using System.Text;  namespace TestHttpWebRequestPostStream {     internal class Program     {         static string m_Authorization = "Your_Authorization";         static int TIMEOUT_TIME = 10000; // 超时时间          static async Task Main(string[] args)         {             string url = "Your_STREAM_URL";             string postData = "Your_POST_DATA";                          string content = await PostRequestStreamToStringAsync(url, postData);              Console.WriteLine("Response content:");             Console.WriteLine(content);         }          static async Task<string> PostRequestStreamToStringAsync(string url, string postData)         {             try             {                 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;                  request.ReadWriteTimeout = TIMEOUT_TIME;                 request.Timeout = TIMEOUT_TIME;                 request.Method = "POST";                 request.ContentType = "application/json"; // 根据需要设定                 request.Headers.Add("user-token", "xxxxxxxx");    // 根据需要设定                 request.Headers.Add("xxxx-authorization", m_Authorization); // 根据需要设定                  //流式发起请求                 using (Stream requestStream = await request.GetRequestStreamAsync())                 {                     byte[] dataBytes = Encoding.UTF8.GetBytes(postData);                     await requestStream.WriteAsync(dataBytes, 0, dataBytes.Length);                 }                  // 流式获取数据响应                 using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())                 using (Stream responseStream = response.GetResponseStream())                 using (StreamReader reader = new StreamReader(responseStream))                 {                     StringBuilder sb = new StringBuilder();                     char[] buffer = new char[1024];                     int bytesRead;                      // 循环获取流式数据                     while ((bytesRead = await reader.ReadAsync(buffer, 0, buffer.Length)) > 0)                     {                         sb.Append(buffer, 0, bytesRead);                         Console.WriteLine(buffer);                         buffer = new char[1024];                     }                      return sb.ToString();                 }             }             catch (Exception e)             {                  return e.Message;             }                      }     } }

附录 : HttpWebRequest 的一些基础知识

1、HttpWebRequest 常用属性

  • AllowAutoRedirect:获取或设置一个值,该值指示请求是否应跟随重定向响应。
  • CookieContainer:获取或设置与此请求关联的cookie。
  • Credentials:获取或设置请求的身份验证信息。
  • KeepAlive:获取或设置一个值,该值指示是否与 Internet 资源建立持久性连接。
  • MaximumAutomaticRedirections:获取或设置请求将跟随的重定向的最大数目。
  • Proxy:获取或设置请求的代理信息。
  • SendChunked:获取或设置一个值,该值指示是否将数据分段发送到 Internet 资源。
  • Timeout:获取或设置请求的超时值。
  • UserAgent:获取或设置 User-agent HTTP 标头的值
  • ContentType:Http内容类型
  • Headers:指定组成 HTTP 标头的名称/值对的集合。
     

2、HttpWebRequest  中的 ContentType

  • 普通文本: “text/plain”
  • JSON字符串: “application/json”
  • 数据流类型(文件流): “application/octet-stream”
  • 表单数据(键值对): “application/x-www-form-urlencoded”
  • 多分部数据: “multipart/form-data”
     

3、HttpWebRequest 一般使用示例

//创建HttpWeb请求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url); //创建HttpWeb相应 HttpWebResponse response = (HttpWebResponse)request.GetResponse ();  Console.WriteLine ("Content length is {0}", response.ContentLength); Console.WriteLine ("Content type is {0}", response.ContentType);  //获取response的流 Stream receiveStream = response.GetResponseStream ();  //使用streamReader读取流数据 StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);  Console.WriteLine ("Response stream received."); Console.WriteLine (readStream.ReadToEnd ()); response.Close (); readStream.Close (); 

4、HttpWebRequest  json 文本 Post 请求示例

使用application/json作为请求头,用来告诉服务端消息主体是序列化的JSON字符串。

/* *  url:POST请求地址 *  postData:json格式的请求报文,例如:{"key1":"value1","key2":"value2"} */ public static string PostUrl(string url, string postData) {     HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);     req.Method = "POST";     req.TimeOut = "800";//设置请求超时时间,单位为毫秒     req.ContentType = "application/json";     byte[] data = Encoding.UTF8.GetBytes(postData);     req.ContentLength = data.Length;      using (Stream reqStream = req.GetRequestStream())     {         reqStream.Write(data, 0, data.Length);         reqStream.Close();     }      HttpWebResponse resp = (HttpWebResponse)req.GetResponse();     Stream stream = resp.GetResponseStream();      //获取响应内容     string result = "";     using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))     {         result = reader.ReadToEnd();     }     return result; } 

5、 HttpWebRequest   multipart/form-data 文件上传示例

使用multipart/form-data作为请求头,用来告诉服务器消息主体是 多发文件 格式

multipart/form-data格式使用一长串字符作为boundtry封装线对字段进行分割。这也很符合multipart多个部分的语义,包含了多个部分集,每一部分都包含了一个content-desposition头,其值为form-data,以及一个name属性,其值为表单的字段名,文件输入框还可以使用filename参数指定文件名。content-type非必须属性,其值会根据文件类型进行变化,默认值是text/plain。multipart的每一个part上方是边界线,最后一个part的下方添加一个边界线。

/// <summary> /// 上传文件请求 /// </summary> /// <param name="url">Url</param> /// <param name="filePath">文件路径</param> /// <param name="formDatas">表单数据(字典格式)</param> /// <param name="callback">上传回调</param> public static void UploadRequest(string url, string filePath, Dictionary<string,string> formDatas, Action<string> callback) {     // 时间戳,用做boundary     string timeStamp = DateTime.Now.Ticks.ToString("x");      //根据uri创建HttpWebRequest对象     HttpWebRequest httpReq = (HttpWebRequest) WebRequest.Create(new Uri(url));     httpReq.Method = "POST";     httpReq.AllowWriteStreamBuffering = false; //对发送的数据不使用缓存     httpReq.Timeout = 300000; //设置获得响应的超时时间(300秒)     httpReq.ContentType = "multipart/form-data; boundary=" + timeStamp;      //读取file文件     FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);     BinaryReader binaryReader = new BinaryReader(fileStream);          //表单信息     string boundary = "--" + timeStamp;     string form = "";     string formFormat = boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n";     string formEnd = boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type:application/octet-stream\r\n\r\n";     foreach (var pair in formDatas)     {         form += string.Format(formFormat, pair.Key, pair.Value);     }     form += string.Format(formEnd,"file", Path.GetFileName(filePath));     byte[] postHeaderBytes = Encoding.UTF8.GetBytes(form);      //结束边界     byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + timeStamp + "--\r\n");     long length = fileStream.Length + postHeaderBytes.Length + boundaryBytes.Length;     httpReq.ContentLength = length; //请求内容长度      try     {         //每次上传4k         int bufferLength = 4096;         byte[] buffer = new byte[bufferLength];          //已上传的字节数         long offset = 0;         int size = binaryReader.Read(buffer, 0, bufferLength);         Stream postStream = httpReq.GetRequestStream();          //发送请求头部消息         postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);          while (size > 0)         {             postStream.Write(buffer, 0, size);             offset += size;             size = binaryReader.Read(buffer, 0, bufferLength);         }          //添加尾部边界         postStream.Write(boundaryBytes, 0, boundaryBytes.Length);         postStream.Close();          //获取服务器端的响应         using (HttpWebResponse response = (HttpWebResponse) httpReq.GetResponse())         {             Stream receiveStream = response.GetResponseStream();             StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);             string returnValue = readStream.ReadToEnd();             Debug.Log("upload result:"+returnValue);             callback?.Invoke(returnValue);                          response.Close();             readStream.Close();         }     }     catch (Exception ex)     {         Debug.Log("文件传输异常: " + ex.Message);     }     finally     {         fileStream.Close();         binaryReader.Close();     } } 

Unity 上的简单的异步封装使用

1、HttpWebRequest 异步 Post 获取数据

using System; using System.IO; using System.Net; using System.Text; using System.Threading.Tasks; using UnityEngine;  public class GptNormalWrapper : Singleton<GptNormalWrapper>, IGptHttpWebRequestWrapper {     const string GPT_URL = "Your_URL";     const string AUTHORIZATION = "Your_AUTHORIZATION";     const string USER_TOKEN = "123456";     const int TIMEOUT_TIME = 10000; // 超时时间      public string GptUrl => GPT_URL;      public async Task<string> PostRequestToStringAsync(string postData) {          return await PostRequestToStringAsync(GptUrl, postData);     }      public async Task<string> PostRequestToStringAsync(string url, string postData)     {         try         {              HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;              request.ReadWriteTimeout = TIMEOUT_TIME;             request.Timeout = TIMEOUT_TIME;             request.Method = "POST";             request.ContentType = "application/json";             request.Headers.Add("user-token", USER_TOKEN);             request.Headers.Add("xxx-authorization", AUTHORIZATION);              Debug.Log("[GptNormalWrapper] PostRequestToStringAsync msg = " + postData);              byte[] jsonBytes = Encoding.UTF8.GetBytes(postData);             request.ContentLength = jsonBytes.Length;             Stream strStream = request.GetRequestStream();             strStream.Write(jsonBytes, 0, jsonBytes.Length);             strStream.Close();              HttpWebResponse respone = (HttpWebResponse)await request.GetResponseAsync();             Stream ns = respone.GetResponseStream();             string responseContent = new StreamReader(ns, Encoding.UTF8).ReadToEnd();              Debug.Log("[GptNormalWrapper] PostRequestToStringAsync 消息返回:" + responseContent);             MessageBack textback = JsonUtility.FromJson<MessageBack>(responseContent);             Debug.Log($"[GptNormalWrapper] PostRequestToStringAsync answer content {textback.data.choices[0].message.content} ");             return new(textback.data.choices[0].message.content);          }         catch (Exception e)         {             Debug.LogError("[GptNormalWrapper] PostRequestToStringAsync error:" + e);         }          return new(GptChatCommonStringDefine.GPT_REQUEST_SERVER_FAILED);     } } 

2、HttpWebRequest 异步 Post 流式的获取数据

 using System.Net; using System.Threading.Tasks; using System.Text; using System.IO; using UnityEngine; using System;   public class GptStreamWrapper : Singleton<GptStreamWrapper>, IGptHttpWebRequestWrapper {     const string GPT_URL = "Your_URL";     const string AUTHORIZATION = "Your_AUTHORIZATION";     const string USER_TOKEN = "123456";     const int TIMEOUT_TIME = 10000; // 超时时间      public string GptUrl { get; } = GPT_URL;      public async Task<string> PostRequestToStringAsync(string postData) {         return await PostRequestToStringAsync(GptUrl, postData);     }      public async Task<string> PostRequestToStringAsync(string url, string postData)     {         try         {             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;              request.ReadWriteTimeout = TIMEOUT_TIME;             request.Timeout = TIMEOUT_TIME;             request.Method = "POST";             request.ContentType = "application/json";             request.Headers.Add("user-token", USER_TOKEN);             request.Headers.Add("xxx-authorization", AUTHORIZATION);              //流式发起请求             using (Stream requestStream = await request.GetRequestStreamAsync())             {                 Debug.Log("[GptStreamWrapper] PostRequestStreamToStringAsync postData : " + postData);                 byte[] dataBytes = Encoding.UTF8.GetBytes(postData);                 await requestStream.WriteAsync(dataBytes, 0, dataBytes.Length);             }              // 流式获取数据响应             using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())             using (Stream responseStream = response.GetResponseStream())             using (StreamReader reader = new StreamReader(responseStream))             {                 StringBuilder sb = new StringBuilder();                 char[] buffer = new char[1024];                 int bytesRead;                  // 循环获取流式数据                 while ((bytesRead = await reader.ReadAsync(buffer, 0, buffer.Length)) > 0)                 {                     sb.Append(buffer, 0, bytesRead);                     Debug.Log("[GptStreamWrapper] PostRequestStreamToStringAsync getting steam data : " + buffer);                     buffer = new char[1024];                 }                  Debug.Log("[GptStreamWrapper] PostRequestStreamToStringAsync whole stream data : " + sb.ToString());                 return sb.ToString();             }         }         catch (Exception e)         {             Debug.Log("[GptStreamWrapper] PostRequestStreamToStringAsync Exception Message data : " + e.Message);             return new(GptChatCommonStringDefine.GPT_REQUEST_SERVER_FAILED);         }      } } 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!