Get请求:参数在链接里面,问号后面的都是参数。

Post请求:参数在表单里面。服务器在底层看见post会自动分配一个大的空间。

1、Get

string url="http://kun.show.ghostry.cn/?int=5";
StartCoroutine(SendGet(url));

Ienumerator SendGet(string url)
{
WWW www=new WWW(url);
yield return www;
//判断下载是否有错
if(string.IsNullOrEmpty(www.error)
{
//TODO
}
}


2、Post

string url="http://kun.show.ghostry.cn/";
WWWForm wwwForm=new WWWForm();
wwwForm.AddField("int",5);
StartCoroutine(SendPost(url,wwwForm));

Ienumerator SendPost(string url,WWWForm form)
{
WWW www=new WWW(url,form);
yield return www;
//判断下载是否有错
if(string.IsNullOrEmpty(www.error)
{
//TODO
}
}

WWW :    还能下载本地资源

IOS:File://

Android: Jar:file://

Pc: File:///

/// <summary>
/// 根据不同平台给url加前缀
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public string Initial(string url)
{
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
{
url = "file:///" + url;

}
else if (Application.platform == RuntimePlatform.Android)
{
url = "jar:file://" + url;
}
else
{
url = "file://" + url;
}
return url;
}

string url = Application.dataPath + "/WWW/WWWTest.cs";
url=Initial(url);
StartCoroutine(SendGet(url));


         Application.dataPath : 这个文件访问工程文件只能在Pc上;

         Application.streamingAssetsPath  :用文件系统可读可写,比如(File,streamRead,streamWrite)

          Android:SD卡

          IOS:Document

          Pc:C:\Users\...\AppData

         Application.persistentDataPath: 这个路径下的所有东西都会打包到真机上面。只能用WWW读,不能写。

         (File,streamRead,streamWrite)不能读写。


WWW封装:

unity http www封装之美_ide

unity http www封装之美_ide_02


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

/// <summary>
/// 注意WWW已经被弃用,可以用UnityWebRequest代替
/// </summary>

//抽象基类
public class WWWItem
{
//开始下载
public virtual void DownloadStart()
{ }
//下载完成
public virtual void DownloadFinish(WWW www)
{ }
//下载出错
public virtual void DownloadError(WWWItem tempItem)
{ }

private string url;

public string URL
{
get
{
return url;
}
set
{
url = value;
}
}
//下载调用
public IEnumerator Download()
{
DownloadStart();
WWW www = new WWW(url);

yield return www;
if (string.IsNullOrEmpty(www.error))
{
DownloadFinish(www);
}
else
{
DownloadError(this);
}
}
}

//下载队列
public class WWWHelp : MonoBehaviour
{
public static WWWHelp Instance;
Queue<WWWItem> allTask;

private void Start()
{
Instance = this;
allTask = new Queue<WWWItem>();
}

bool isDownloadingFinish = true;
//下载任务入队列
public void AddTask(WWWItem item)
{
allTask.Enqueue(item);
if(allTask.Count==1 && isDownloadingFinish)
{
isDownloadingFinish = false;
StartCoroutine(DownloadItem());
}
}
public IEnumerator DownloadItem()
{
while(allTask.Count>0)
{
WWWItem item= allTask.Dequeue();
yield return item.Download();
}
isDownloadingFinish=true;
}
}

public class WWWTxt:WWWItem
{

public WWWTxt(string url)
{
Initial(url);
}

public void Initial(string url)
{
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer)
{
this.URL = "file:///" + url;

}
else if (Application.platform == RuntimePlatform.Android)
{
this.URL = "jar:file://" + url;
}
else
{
this.URL = "file://" + url;
}

}


public override void DownloadStart()
{
//base.DownloadStart();
Debug.Log("start download.");
}

public override void DownloadFinish(WWW www)
{
//base.DownloadFinish(www);
Debug.Log("finish download" + www.text);
//解析 解压在这里
}

public override void DownloadError(WWWItem tempItem)
{
// base.DownloadError(tempItem);
WWWHelp.Instance.AddTask(tempItem);
}
}



public class WWWTest:MonoBehaviour
{
private void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
string url = "bulabula";
WWWTxt tempText = new WWWTxt(url);
WWWHelp.Instance.AddTask(tempText);
}
}
}