Example #1
0
        private static byte[] GetMultipartFormData(IDictionary <string, object> postParameters, string boundary)
        {
            Stream formDataStream = new System.IO.MemoryStream();
            bool   needsCLRF      = false;

            foreach (var param in postParameters)
            {
                // Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
                // Skip it on the first parameter, add it to subsequent parameters.
                if (needsCLRF)
                {
                    formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));
                }

                needsCLRF = true;

                if (param.Value is FileParameter)
                {
                    FileParameter fileToUpload = (FileParameter)param.Value;

                    // Add just the first part of this param, since we will write the file data directly to the Stream
                    string header = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\";\r\nContent-Type: {3}\r\n\r\n",
                                                  boundary,
                                                  param.Key,
                                                  fileToUpload.FileName ?? param.Key,
                                                  fileToUpload.ContentType ?? "application/octet-stream");

                    formDataStream.Write(encoding.GetBytes(header), 0, encoding.GetByteCount(header));

                    // Write the file data directly to the Stream, rather than serializing it to a string.
                    formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
                }
                else
                {
                    string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"\r\n\r\n{2}",
                                                    boundary,
                                                    param.Key,
                                                    param.Value);
                    formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
                }
            }

            // Add the end of the request.  Start with a newline
            string footer = "\r\n--" + boundary + "--\r\n";

            formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));

            // Dump the Stream into a byte[]
            formDataStream.Position = 0;
            byte[] formData = new byte[formDataStream.Length];
            formDataStream.Read(formData, 0, formData.Length);

            GameSparksUtil.CompleteStream(formDataStream);

            return(formData);
        }
Example #2
0
        private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                IDictionary <string, object> asyncResultValues = (IDictionary <string, object>)asynchronousResult.AsyncState;

                HttpWebRequest request       = (HttpWebRequest)asyncResultValues["r"];
                byte[]         formData      = (byte[])asyncResultValues["d"];
                Stream         requestStream = request.EndGetRequestStream(asynchronousResult);
                requestStream.Write(formData, 0, formData.Length);
                GameSparksUtil.CompleteStream(requestStream);
                request.BeginGetResponse(new AsyncCallback(GetResponseCallback), asyncResultValues);
            }
            catch (Exception e)
            {
                GS.GSPlatform.DebugMsg(e.ToString());
            }
        }