Beispiel #1
0
        private static void ConfigureRESTfulApi(RestMethodInfo methodInfo, HttpClientRequest client)
        {
            //add headers
            if (methodInfo.Headers.Count > 0)
            {
                foreach (var keyValuePair in methodInfo.Headers)
                {
                    client.Request.Headers.Add(keyValuePair.Key, keyValuePair.Value);
                }
            }
            if (methodInfo.HeaderParameterMap.Count > 0)
            {
                foreach (var keyValuePair in methodInfo.HeaderParameterMap)
                {
                    client.Request.Headers.Add(keyValuePair.Key, keyValuePair.Value);
                }
            }
            HttpAction httpAction = (HttpAction)Enum.Parse(typeof(HttpAction), methodInfo.Method.ToString());

            client.SetMethod(httpAction);
            switch (httpAction)
            {
            case HttpAction.Delete:
                break;

            case HttpAction.Get:
                break;

            case HttpAction.Patch:
            case HttpAction.Post:
            case HttpAction.Put:
                MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();
                FormUrlEncodedContent    formUrlEncodedContent    = null;
                ByteArrayContent         byteArrayContent         = null;
                if (methodInfo.IsMultipart && methodInfo.GotPart && methodInfo.Part != null)
                {
                    //multipart bytes
                    byteArrayContent = new ByteArrayContent(methodInfo.Part.GetBinaryData(), methodInfo.Part.Mimetype);
                }
                if (methodInfo.FieldParameterMap.Count > 0)
                {
                    //field
                    var fields = new Dictionary <string, string>();
                    foreach (var keyValuePair in methodInfo.FieldParameterMap)
                    {
                        fields.Add(keyValuePair.Key, keyValuePair.Value.ToString());
                    }
                    formUrlEncodedContent = new FormUrlEncodedContent(fields);
                }

                if (byteArrayContent != null)
                {
                    //multipart/form-data; boundary=***
                    multipartFormDataContent.Add(byteArrayContent, methodInfo.Part.Field, methodInfo.Part.FileName);
                    if (formUrlEncodedContent != null)
                    {
                        foreach (var keyValuePair in methodInfo.FieldParameterMap)
                        {
                            StringContent stringContent = new StringContent(keyValuePair.Value.ToString());
                            multipartFormDataContent.Add(stringContent, keyValuePair.Key);
                        }
                        client.SetHttpContent(multipartFormDataContent);
                    }
                }
                else if (formUrlEncodedContent != null)
                {
                    client.SetHttpContent(formUrlEncodedContent);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            if (!string.IsNullOrEmpty(methodInfo.bodyString))
            {
                //raw body application/json
                StringContent content = new StringContent(methodInfo.bodyString, Encoding.UTF8, "application/json");
                client.SetHttpContent(content);
            }
//          client.SetProxy(new WebProxy(new Uri("http://127.0.0.1:8888")));
        }