Ejemplo n.º 1
0
        public HttpPlasmaResponse GenerateFormPostRequest(HttpPlasmaClient client)
        {
            // path and query string
            var headers = new WebHeaderCollection();
            string path;
            string query;

            var iQuery = _action.IndexOf('?');

            if (iQuery >= 0) {
                path = _action.Substring(0, iQuery);
                query = _action.Substring(iQuery + 1);
            }
            else {
                path = _action;
                query = null;
            }

            if (_fileControls.Count > 0)
            {
                var multipartFormBody = new MultipartFormBody(this, _fileControls);

                headers.Add(HttpRequestHeader.ContentLength, multipartFormBody.ContentLength);
                headers.Add(HttpRequestHeader.ContentType, multipartFormBody.ContentType);
                var body = multipartFormBody.FormBodyData();
                return client.Post(path, query, body, headers);
            }

            var formData = GenerateFormDataAsString();

            if (string.Compare(_method, "GET", StringComparison.OrdinalIgnoreCase) == 0) {
                return client.Get(path, formData);
            }

            var formBody = Encoding.UTF8.GetBytes(formData);
            headers.Add(HttpRequestHeader.ContentLength, formBody.Length.ToString());
            headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
            return client.Post(path, query, formBody, headers);
        }
Ejemplo n.º 2
0
        public AspNetRequest GenerateFormPostRequest()
        {
            // path and query string

            string path;
            string query;

            int iQuery = _action.IndexOf('?');

            if (iQuery >= 0) {
                path = _action.Substring(0, iQuery);
                query = _action.Substring(iQuery + 1);
            }
            else {
                path = _action;
                query = null;
            }

            var headers = new List<KeyValuePair<string, string>>();

            if (_fileControls.Count > 0)
            {
                var multipartFormBody = new MultipartFormBody(this, _fileControls);
                headers.Add(new KeyValuePair<string, string>("Content-Length", multipartFormBody.ContentLength));
                headers.Add(new KeyValuePair<string, string>("Content-Type", multipartFormBody.ContentType));
                return new AspNetRequest(path, null, query, "POST", headers, multipartFormBody.FormBodyData());
            }

            // form collection as string

            string formData = GenerateFormDataAsString();

            // form data as query string or body (depending on method)

            string verb;
            byte[] formBody = null;

            if (string.Compare(_method, "GET", StringComparison.OrdinalIgnoreCase) == 0) {
                verb = "GET";

                // for GET requests form goes into query string
                query = formData;
            }
            else {
                verb = "POST";

                // for POST requests form goes into request body
                formBody = Encoding.UTF8.GetBytes(formData);
                headers.Add(new KeyValuePair<string, string>("Content-Length", formBody.Length.ToString()));
                headers.Add(new KeyValuePair<string, string>("Content-Type", "application/x-www-form-urlencoded"));
            }

            // create the request based on the above

            return new AspNetRequest(path, null, query, verb, headers, formBody);
        }