Unity Http发送 multipart/form 请求

RestClient

  1. Github 地址
  2. 默认格式
    restClient 默认发送数据的格式是 json
  3. multipart/form 格式
    void Test () {
      WWWForm form = new WWWForm();
        
        form.AddField("accountName", "lvpeilinlove");
        form.AddField("password","123456");
        form.AddField("platformId","0");
        form.AddField("lang","1");
        form.AddField("cityCode","131");
    
        RestClient.Post( new RequestHelper()
        {
            Uri = url, 
            DefaultContentType = false,
            FormData = form
        }).Then(response => {
           EditorUtility.DisplayDialog("Response", response.Text, "Ok");
        });
    }
    

UnityWebRequest

public void Start()
{
    StartCoroutine(PostRequest(url));
}

IEnumerator PostRequest(string uri)
{
    WWWForm form = new WWWForm();
    
    form.AddField("accountName", "lvpeilinlove");
    form.AddField("password","123456");
    form.AddField("platformId","0");
    form.AddField("lang","1");
    form.AddField("cityCode","131");
    
    using (UnityWebRequest webRequest = UnityWebRequest.Post(uri, form))
    {
        yield return webRequest.SendWebRequest();
    
        string[] pages = uri.Split('/');
        int page = pages.Length - 1;
    
        switch (webRequest.result)
        {
            case UnityWebRequest.Result.ConnectionError:
            case UnityWebRequest.Result.DataProcessingError:
                Debug.LogError(pages[page] + ": Error: " + webRequest.error);
                break;
            case UnityWebRequest.Result.ProtocolError:
                Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
                break;
            case UnityWebRequest.Result.Success:
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
                break;
        }
    }
}

[1] Post三种方式

你可能感兴趣的:(问题记录,C#,unity,http,unity)