HTTP简单的接收和发送

void Start()
    {
        StartCoroutine(GetRequest("https://jsonplaceholder.typicode.com/posts/1"));
        StartCoroutine(PostRequest("https://jsonplaceholder.typicode.com","hahah"));
    }

    /// 
    /// 获取网页上的信息
    /// 
    /// 
    /// 
    IEnumerator GetRequest(string uri)
    {
        using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
        {
            yield return webRequest.SendWebRequest();

            if (webRequest.result == UnityWebRequest.Result.Success)
            {
                Debug.Log("Received: " + webRequest.downloadHandler.text);
            }
            else
            {
                Debug.Log("Error: " + webRequest.error);
            }
        }
    }

    /// 
    /// 发送信息到网页
    /// 
    /// 
    /// 
    /// 
    IEnumerator PostRequest(string uri, string data)
    {
        List formData = new List();
        formData.Add(new MultipartFormDataSection("field1=foo&field2=bar"));

        UnityWebRequest www = UnityWebRequest.Post(uri, formData);
        yield return www.SendWebRequest();

        if (www.result == UnityWebRequest.Result.Success)
        {
            Debug.Log("Post request successful!");
        }
        else
        {
            Debug.Log("Error: " + www.error);
        }
    }

你可能感兴趣的:(http,网络协议,网络,c#)