Пример #1
0
        private void SendWebRequestAsync <TResponse>(string httpMethod, object request,
                                                     AsyncState <TResponse> state, HttpWebRequest client)
        {
            client.Accept = ContentType;

            if (this.EmulateHttpViaPost)
            {
                client.Method = "POST";
                client.Headers[HttpHeaders.XHttpMethodOverride] = httpMethod;
            }
            else
            {
                client.Method = httpMethod;
            }

            PclExportClient.Instance.AddHeader(client, Headers);

            //EmulateHttpViaPost is also forced for SL5 clients sending non GET/POST requests
            PclExport.Instance.Config(client, userAgent: UserAgent);

            if (this.authInfo != null && !string.IsNullOrEmpty(this.UserName))
            {
                client.AddAuthInfo(this.UserName, this.Password, authInfo);
            }
            else if (this.BearerToken != null)
            {
                client.Headers[HttpHeaders.Authorization] = "Bearer " + this.BearerToken;
            }
            else if (this.Credentials != null)
            {
                client.Credentials = this.Credentials;
            }
            else if (this.AlwaysSendBasicAuthHeader)
            {
                client.AddBasicAuth(this.UserName, this.Password);
            }

            ApplyWebRequestFilters(client);

            try
            {
                if (client.Method.HasRequestBody())
                {
                    client.ContentType = ContentType;
                    client.BeginGetRequestStream(RequestCallback <TResponse>, state);
                }
                else
                {
                    state.WebRequest.BeginGetResponse(ResponseCallback <TResponse>, state);
                }
            }
            catch (Exception ex)
            {
                // BeginGetRequestStream can throw if request was aborted
                HandleResponseError(ex, state);
            }
        }
        private void SendWebRequestAsync <TResponse>(string httpMethod, object request,
                                                     RequestState <TResponse> requestState, HttpWebRequest webRequest)
        {
            var httpGetOrDeleteOrHead = (httpMethod == "GET" || httpMethod == "DELETE" || httpMethod == "HEAD");

            webRequest.Accept = string.Format("{0}, */*", ContentType);

#if !SILVERLIGHT
            webRequest.Method = httpMethod;
#else
            //Methods others than GET and POST are only supported by Client request creator, see
            //http://msdn.microsoft.com/en-us/library/cc838250(v=vs.95).aspx

            if (this.UseBrowserHttpHandling && httpMethod != "GET" && httpMethod != "POST")
            {
                webRequest.Method = "POST";
                webRequest.Headers[HttpHeaders.XHttpMethodOverride] = httpMethod;
            }
            else
            {
                webRequest.Method = httpMethod;
            }
#endif

            if (this.Credentials != null)
            {
                webRequest.Credentials = this.Credentials;
            }
            if (this.AlwaysSendBasicAuthHeader)
            {
                webRequest.AddBasicAuth(this.UserName, this.Password);
            }

            ApplyWebRequestFilters(webRequest);

            try
            {
                if (!httpGetOrDeleteOrHead && request != null)
                {
                    webRequest.ContentType = ContentType;
                    webRequest.BeginGetRequestStream(RequestCallback <TResponse>, requestState);
                }
                else
                {
                    requestState.WebRequest.BeginGetResponse(ResponseCallback <TResponse>, requestState);
                }
            }
            catch (Exception ex)
            {
                // BeginGetRequestStream can throw if request was aborted
                HandleResponseError(ex, requestState);
            }
        }
Пример #3
0
        private void SendWebRequestAsync <TResponse>(string httpMethod, object request,
                                                     AsyncState <TResponse> state, HttpWebRequest webRequest)
        {
            webRequest.Accept = string.Format("{0}, */*", ContentType);

            //Methods others than GET and POST are only supported by Client request creator, see
            //http://msdn.microsoft.com/en-us/library/cc838250(v=vs.95).aspx
            if (this.EmulateHttpViaPost && httpMethod != "GET" && httpMethod != "POST")
            {
                webRequest.Method = "POST";
                webRequest.Headers[HttpHeaders.XHttpMethodOverride] = httpMethod;
            }
            else
            {
                webRequest.Method = httpMethod;
            }

            PclExportClient.Instance.AddHeader(webRequest, Headers);

            PclExport.Instance.Config(webRequest, userAgent: UserAgent);

            if (this.Credentials != null)
            {
                webRequest.Credentials = this.Credentials;
            }
            if (this.AlwaysSendBasicAuthHeader)
            {
                webRequest.AddBasicAuth(this.UserName, this.Password);
            }

            ApplyWebRequestFilters(webRequest);

            try
            {
                if (httpMethod.HasRequestBody() && request != null)
                {
                    webRequest.ContentType = ContentType;
                    webRequest.BeginGetRequestStream(RequestCallback <TResponse>, state);
                }
                else
                {
                    state.WebRequest.BeginGetResponse(ResponseCallback <TResponse>, state);
                }
            }
            catch (Exception ex)
            {
                // BeginGetRequestStream can throw if request was aborted
                HandleResponseError(ex, state);
            }
        }
Пример #4
0
        private void SendWebRequestAsync <TResponse>(string httpMethod, object request,
                                                     AsyncState <TResponse> state, HttpWebRequest webRequest)
        {
            webRequest.Accept = string.Format("{0}, */*", ContentType);

            if (this.EmulateHttpViaPost)
            {
                webRequest.Method = "POST";
                webRequest.Headers[HttpHeaders.XHttpMethodOverride] = httpMethod;
            }
            else
            {
                webRequest.Method = httpMethod;
            }

            PclExportClient.Instance.AddHeader(webRequest, Headers);

            //EmulateHttpViaPost is also forced for SL5 clients sending non GET/POST requests
            PclExport.Instance.Config(webRequest, userAgent: UserAgent);

            if (this.Credentials != null)
            {
                webRequest.Credentials = this.Credentials;
            }
            if (this.AlwaysSendBasicAuthHeader)
            {
                webRequest.AddBasicAuth(this.UserName, this.Password);
            }

            ApplyWebRequestFilters(webRequest);

            try
            {
                if (webRequest.Method.HasRequestBody())
                {
                    webRequest.ContentType = ContentType;
                    webRequest.BeginGetRequestStream(RequestCallback <TResponse>, state);
                }
                else
                {
                    state.WebRequest.BeginGetResponse(ResponseCallback <TResponse>, state);
                }
            }
            catch (Exception ex)
            {
                // BeginGetRequestStream can throw if request was aborted
                HandleResponseError(ex, state);
            }
        }
Пример #5
0
 private void SetBasicAuthRequest(HttpWebRequest req)
 {
     req.AddBasicAuth(Creds.Username, Creds.Password);
     req.Headers["X-CSRF-Token"] = CsrfToken;
 }