在Unity中,使用system.net中的HttpWebRequest获取返回信息,如果数据较大,会卡顿,主要由于unity单线程,httpwebrequest不能异步执行;
解决办法,可以使用unitywebrequest,结合uploadhandler、downloadhandler协程处理;
Unity 5.5:
byte[] bs=Encoding.UTF8.GetBytes(“string”);
UnityWebRequest request=new UnityWebRequest(url, method);
UploadHandler uphandler=new UploadHandlerRaw(bs);
uphandler.contentType="Text/xml;charset=utf-8";
request.uploadhandler=uphandler;
request.downloadhandler=new DownloadHandlerBuffer();
yield return request.sent();
while(!request.isDone){
yield return null;
}
if(!request.isError){
//string
string text=request.downloadhandler.text;
//rawdata
byte[] data=request.downloadhandler.data;
}