一个简单的Post Get请求

WWW请求

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

public class WWWWebRequest : MonoBehaviour
{
    enum WebRequestType
    {
        GET,
        POST_FORM,
        POST_URLENCONDING,
        POST_JSON,
        POST_XML
    }

    public static WWWWebRequest Instance;

    public void Get(string url,Action<WWW> actionGet)
    {
        StartCoroutine(Request(url,null, actionGet,WebRequestType.GET));
    }

    public void PostForm(string url, WWWForm form, Action<WWW> actionPost)
    {
        StartCoroutine(Request(url, form, actionPost, WebRequestType.POST_FORM));
    }

    IEnumerator Request(string url,WWWForm form,Action<WWW> action,WebRequestType type)
    {
        WWW www =  null;

        switch (type)
        {
            case WebRequestType.GET:
                www = new WWW(url);
                break;
            case WebRequestType.POST_FORM:   
                www = new WWW(url, form);  
                break;
            default:
                break;
        }

        if(www==null)
        {
            Debug.Log("Cant config request paramater");
            yield break;
        }

        yield return www;

        action?.Invoke(www);

        www.Dispose();
        www = null;
        Resources.UnloadUnusedAssets();
    }

    private void Awake()
    {
        Instance = this;
    }
}

 

posted @ 2018-09-30 10:20  81192  阅读(579)  评论(3编辑  收藏  举报