开发者社区> 问答> 正文

OSS上传文件400错误,提示MalformedPOSTRequest

用unity3d的WWWForm会失败,自己构造的表单可以成功。
fiddler抓取的数据:



1:WWWForm的表单多出了Content-Type: text/plain; charset="utf-8"
2:自己构造表单的时候加上Content-Type: text/plain; charset="utf-8"也会失败。

展开
收起
gamepdc 2016-05-30 18:49:40 8335 0
2 条回答
写回答
取消 提交回答
  • ReOSS上传文件400错误,提示MalformedPOSTRequest
    Unity3D模仿WWW实现的兼容OSS的Multipart上传类
    public class MultiPartUpload
    {
        public bool isDone;
        public bool isSuccess;
        public string rspString;


        private HttpWebRequest request;
        private HttpWebResponse response;
        private Stream streamResponse;


        private string mUrl;
        private Dictionary<string, string> mKeyValues;
        private string mFileName;
        private byte[] mFileData;
        private byte[] mPostData;
        private string mBoundary;


        private IAsyncResult mResult;


        public MultiPartUpload(string url, Dictionary<string, string> keyValues, string filename, byte[] data)
        {
            streamResponse = null;
            isDone = false;
            isSuccess = false;
            rspString = "";


            mUrl = url;
            mKeyValues = keyValues;
            mFileName = filename;
            mFileData = data;


            mBoundary = "--------9431149156168";


            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Timeout = -1;
            webRequest.Method = "POST";
            webRequest.ContentType = "multipart/form-data; boundary=" + mBoundary;
            this.request = webRequest;
            mResult = webRequest.BeginGetRequestStream(getRequestStreamCallback, this);
        }


        private void getRequestStreamCallback(IAsyncResult ar)
        {
            try
            {
                Stream postStream = request.EndGetRequestStream(ar);
                string boundary = mBoundary;
                
                string requestBody = "--" + boundary + "\r\n";
                foreach(KeyValuePair<string, string> keyValue in mKeyValues)
                {
                    string postField = "Content-Disposition: form-data; name=\"" + keyValue.Key + "\"\r\n"
                        + "\r\n" + keyValue.Value + "\r\n"
                        + "--" + boundary + "\r\n";
                    requestBody += postField;
                }
                string tail = "\r\n" + "--" + boundary + "--\r\n";


                List<byte> postData = new List<byte>();
                {
                    postData.AddRange(System.Text.Encoding.Default.GetBytes(requestBody));
                    string fileString = "Content-Disposition: form-data; name=\"file\"; filename=\"" + mFileName + "\"\r\n\r\n";
                    postData.AddRange(System.Text.Encoding.Default.GetBytes(fileString));
                    postData.AddRange(mFileData);
                    postData.AddRange(System.Text.Encoding.Default.GetBytes(tail));
                }
                mPostData = postData.ToArray();


                postStream.Write(mPostData, 0, mPostData.Length);
                postStream.Close();
                mResult = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(getResponseCallback), this);
            }
            catch (Exception e)
            {
                rspString = e.Message;
                isDone = true;
            }
        }


        private void getResponseCallback(IAsyncResult ar)
        {
            try
            {
                HttpWebResponse rsp = (HttpWebResponse)request.EndGetResponse(ar);
                Stream streamRsp = rsp.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamRsp);


                rspString = streamRead.ReadToEnd();
                isSuccess = rsp.StatusCode == HttpStatusCode.OK;
                isDone = true;


                streamRsp.Close();
                streamRead.Close();
                rsp.Close();
            }
            catch (Exception e)
            {
                rspString = e.Message;
                isDone = true;
            }
        }
    }
    2016-05-31 20:44:09
    赞同 展开评论 打赏
  • 阿里云对象存储(OSS)开发工程师
    unity3d中构造的表单每一个表单域都到有content-type,oss不接受这样的请求,所以报错了
    2016-05-30 21:30:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
OSS运维进阶实战手册 立即下载
《OSS运维基础实战手册》 立即下载
OSS运维基础实战手册 立即下载