Example #1
0
        internal static string ExecuteMultipartFormPostRequest(
          HttpWebRequest webRequest,
          Dictionary<string, string> postData,
          Stream fileToUpload,
          string fileNameToUpload,
          string fileMimeType,
          string fileFormKey)
        {
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.KeepAlive = true;

            //Creates a multipart/form-data boundary.
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            Stream requestStream = webRequest.GetRequestStream();
            postData.WriteMultipartFormData(requestStream, boundary);
            if (fileToUpload != null)
            {
                fileToUpload.WriteMultipartFormData(fileNameToUpload, requestStream, boundary, fileMimeType, fileFormKey);
            }
            byte[] endBytes = Encoding.UTF8.GetBytes("--" + boundary + "--");
            requestStream.Write(endBytes, 0, endBytes.Length);
            requestStream.Close();

            try
            {
                using (WebResponse response = webRequest.GetResponse())
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                };
            }
            catch (WebException webException)
            {
                if (webException.Response != null)
                {
                    var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                    var stripeError = new StripeError();

                    if (webRequest.RequestUri.ToString().Contains("oauth"))
                        stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()));
                    else
                        stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");

                    throw new StripeException(statusCode, stripeError, stripeError.Message);
                }

                throw;
            }
        }
Example #2
0
        internal static string ExecuteMultipartFormPostRequest(
          HttpWebRequest webRequest,
          Dictionary<string, string> postData,
          Stream fileToUpload,
          string fileNameToUpload,
          string fileMimeType,
          string fileFormKey, StripeRequestOptions requestOptions = null)
        {
            string result = null;

            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.KeepAlive = true;

            //Creates a multipart/form-data boundary.
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");

            webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
            Stream requestStream = webRequest.GetRequestStream();
            postData.WriteMultipartFormData(requestStream, boundary);
            if (fileToUpload != null)
            {
                fileToUpload.WriteMultipartFormData(fileNameToUpload, requestStream, boundary, fileMimeType, fileFormKey);
            }
            byte[] endBytes = Encoding.UTF8.GetBytes("--" + boundary + "--");
            requestStream.Write(endBytes, 0, endBytes.Length);
            requestStream.Close();

            // Attempt communication up to 3 times, in case of temporary Stripe outtage or other reason
            // Idempotency string on charges will prevent multiple transactions
            const int maxAttempts = 3;

            for (var attempt = 1; attempt <= maxAttempts; attempt++)
            {
                try
                {
                    using (WebResponse response = webRequest.GetResponse())
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        result = reader.ReadToEnd();
                    };
                }
                catch (WebException webException)
                {
                    if (webException.Response != null)
                    {
                        var statusCode = ((HttpWebResponse)webException.Response).StatusCode;

                        var stripeError = new StripeError();

                        if (webRequest.RequestUri.ToString().Contains("oauth"))
                            stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()));
                        else
                            stripeError = Mapper<StripeError>.MapFromJson(ReadStream(webException.Response.GetResponseStream()), "error");

                        if (attempt == maxAttempts)
                            throw new StripeException(statusCode, stripeError, stripeError.Message);
                    }

                    if (attempt == maxAttempts)
                        throw;
                }

                if (!string.IsNullOrEmpty(result))
                    break;
            }

            return result;
        }
Example #3
0
        // success: return the location of the new resource
        // failure: return empty string
        // use this function to post multipart data, where postData contains each segment
        // and fileToUpload is the file to upload
        // TODO make one POST function instead of two
        public static XmlDocument PostResource(string uri, Dictionary<string, string> postData,
			FileInfo fileToUpload, string fileMimeType, string fileFormKey)
        {
            string authUri = Authentication.PrepareAuthenticatedUri(uri);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(authUri);
              			request.Method = "POST";
              			request.KeepAlive = true;
              			string boundary = MultipartBoundary.CreateFormDataBoundary();
              			request.ContentType = "multipart/form-data; boundary=" + boundary;
              			Stream requestStream = request.GetRequestStream();
              			postData.WriteMultipartFormData(requestStream, boundary);
              	if (fileToUpload != null)
              	{
                fileToUpload.WriteMultipartFormData(requestStream, boundary, fileMimeType, fileFormKey);
              	}
              	byte[] endBytes = System.Text.Encoding.UTF8.GetBytes("--" + boundary + "--");
              	requestStream.Write(endBytes, 0, endBytes.Length);
              	requestStream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            System.Diagnostics.Debug.Print(String.Format("Received status code: {0}", response.StatusCode.ToString()));

              			// 201
            if (response.StatusCode == HttpStatusCode.Created)
            {
                Stream receiveStream = response.GetResponseStream();
                string s = NormalizeData(receiveStream);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(s);
                return doc;
            }
            // 400
            else if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                return null;
            }
            else
            {
                return null;
            }
        }
Example #4
0
		public static Task<string> PostMultipartFormString(string url, Dictionary<string, string> data, Stream file, string name, string mimeType, string formKey)
		{
			var request = (HttpWebRequest)CreateWebRequest(url, POST);

			request.ContentType = "application/x-www-form-urlencoded";
			request.KeepAlive = true;

			//Creates a multipart/form-data boundary.
			var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
			request.ContentType = "multipart/form-data; boundary=" + boundary;

			using (var requestStream = request.GetRequestStream())
			{
				data.WriteMultipartFormData(requestStream, boundary);
				if (file != null)
				{
					file.WriteMultipartFormData(name, requestStream, boundary, mimeType, formKey);
				}

				var endBytes = Encoding.UTF8.GetBytes("--" + boundary + "--");
				requestStream.Write(endBytes, 0, endBytes.Length);
				requestStream.Close();
			}

			return ExecuteWebRequestAsync(request);
		}