Ejemplo n.º 1
0
        /// <summary>
        /// 发送POST 方式的请求
        /// </summary>
        /// <param name="onRequestFinishedDelegate">回调函数</param>
        /// <param name="urlString">请求地址</param>
        /// <param name="paramsDict">请求的普通参数</param>
        /// <param name="parameterModel">请求需要发送的文件数据</param>
        /// <returns>请求对象</returns>
        private HTTPRequest SendPostType(OnRequestFinishedDelegate onRequestFinishedDelegate, string urlString,
                                         Dictionary <string, string> paramsDict, params FileParamModel[] parameterModel)
        {
            HTTPRequest request = new HTTPRequest(new Uri(urlString), HTTPMethods.Post, onRequestFinishedDelegate);



            //普通Post参数

            //文件参数
            if (parameterModel != null && parameterModel.Length > 0)
            {
                foreach (FileParamModel model in parameterModel)
                {
                    request.AddBinaryData(model.FieldName, model.Content, model.FileName, model.MimeType);
                }
                if (paramsDict != null && paramsDict.Count > 0)
                {
                    request.SetHeader("Content-Type", "application/json");
                    foreach (KeyValuePair <string, string> keyValuePair in paramsDict)
                    {
                        request.AddField(keyValuePair.Key, keyValuePair.Value);
                    }
                }
                Debug.Log("有文件参数" + parameterModel.Length);
            }
            else
            {
                request.SetHeader("Content-Type", "application/json");
                RequestModel requestModel = new RequestModel();
                if (paramsDict != null && paramsDict.Count > 0)
                {
                    //foreach (KeyValuePair<string, string> keyValuePair in paramsDict)
                    //{
                    //    requestModel.AddData(keyValuePair.Key, keyValuePair.Value);
                    //}

                    requestModel.data = paramsDict;
                }
                Debug.Log(requestModel.ObjectToJson());
                request.RawData = requestModel.GetBytes();
            }
            //发送
            return(request.Send());
        }