資源簡介
unity的相關:Http請求主要是通過向服務器發送json數據,服務器返回json數據。該代碼中有POST主方法,并且還提供json和對象類型進行相互轉化的方法,方便開發者使用Unity 中Http 的POST請求。使用方便,快捷,只需要建立相應的參數對象和返回結果對象直接調用即可。

代碼片段和文件信息
using?System.Collections;
using?System.Collections.Generic;
using?UnityEngine;
using?UnityEngine.UI;
using?UnityEngine.Events;
using?System.Net;
using?System.IO;
using?System.Net.Security;
using?System.Text;
using?Newtonsoft.Json;
///?
///?POST網絡請求
///?
public?class?HTTPNetTool
{
///?
///?網絡請求主方法
///?
///?返回json結果,可以通過下面Deserialize?方法轉換成對象直接解析數據。
///?
///?參數字符串,通過下面的Serialize方法,由類轉換得到。
public?static?string?PostMoths(string?url?string?param)
{
string?strURL?=?url;
System.Net.HttpWebRequest?request;
request?=?(System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Timeout?=?200000;
request.Method?=?“POST“;
request.ContentType?=?“application/json;charset=UTF-8“;
string?paraUrlCoded?=?param;
byte[]?payload;
payload?=?System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength?=?payload.Length;
Stream?writer?=?request.GetRequestStream();
writer.Write(payload?0?payload.Length);
writer.Close();
System.Net.HttpWebResponse?response;
response?=?(System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream?s;
s?=?response.GetResponseStream();
string?StrDate?=?““;
string?strValue?=?““;
StreamReader?Reader?=?new?StreamReader(s?Encoding.UTF8);
while?((StrDate?=?Reader.ReadLine())?!=?null)
{
strValue?+=?StrDate?+?“\r\n“;
}
return?strValue;
}
///?
///?把json字符串轉成對象
///?
///?對象
///?json字符串?
public?static?T?Deserialize(string?data)
{
System.Web.script.Serialization.javascriptSerializer?json?=?new?System.Web.script.Serialization.javascriptSerializer();
return?json.Deserialize(data);
}
///?
///?把對象轉成json字符串
///?
///?對象
///?json字符串
public?static?string?Serialize(object?o)
{
System.Text.StringBuilder?sb?=?new?System.Text.StringBuilder();
System.Web.script.Serialization.javascriptSerializer?json?=?new?System.Web.script.Serialization.javascriptSerializer();
json.Serialize(o?sb);
return?sb.ToString();
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????2368??2017-11-09?10:45??HTTPNetTool.cs
- 上一篇:vs2005學生信息管理系統
- 下一篇:華為軟件編程規范和范例
評論
共有 條評論