示例#1
0
        public HandledHttpWebResponse Post(string url, XHttpMethod method, object body = null)
        {
            _initTask.Wait();

            var uri     = string.Concat(_baseUrl, _siteUrl, "/", url);
            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Method = "POST";
            request.Accept = "application/json;odata.metadata=none";
            request.Headers["X-RequestDigest"] = _formDigestValue;
            request.CookieContainer            = _authorizationCookies;

            if (method != XHttpMethod.CREATE)
            {
                request.Headers["X-HTTP-Method"] = method.GetDescription();
                request.Headers["If-Match"]      = "*";
            }

            if (body != null)
            {
                request.AddBody(body, "application/json;odata=verbose");
            }
            else
            {
                request.ContentLength = 0;
            }

            return(request.GetHandledResponse());
        }
 public async Task<string> SendAsync(string url, XHttpMethod httpMethod, string data = "")
 {
     string result = String.Empty;
     using (var httpClient = new HttpClient())
     {
         switch (httpMethod)
         {
             case XHttpMethod.POST:
                 var content = new StringContent("data=" + data, UnicodeEncoding.UTF8,
                     "application/x-www-form-urlencoded");
                 using (var message = await httpClient.PostAsync(url, content))
                 {
                     result = await message.Content.ReadAsStringAsync();
                 }
                 break;
             case XHttpMethod.GET:
                 var urlWithData = String.IsNullOrEmpty(data) ? url : url + "?data=" + data;
                 using (var message = await httpClient.GetAsync(urlWithData))
                 {
                     result = await message.Content.ReadAsStringAsync();
                 }
                 break;
             case XHttpMethod.PUT:
                 var contentPut = new StringContent("data=" + data, UnicodeEncoding.UTF8,
                     "application/x-www-form-urlencoded");
                 using (var message = await httpClient.PutAsync(new Uri(url), contentPut))
                 {
                     result = await message.Content.ReadAsStringAsync();
                 }
                 break;
             case XHttpMethod.DELETE:
                 var deleteMsg = new HttpRequestMessage
                 {
                     Method = new HttpMethod("DELETE"),
                     RequestUri = new Uri(url),
                     Content =
                         new StringContent("data=" + data, UnicodeEncoding.UTF8,
                             "application/x-www-form-urlencoded")
                 };
                 using (
                     var message =
                         await httpClient.SendAsync(deleteMsg, HttpCompletionOption.ResponseContentRead))
                 {
                     result = await message.Content.ReadAsStringAsync();
                 }
                 break;
             case XHttpMethod.UPLOADDATA:
                 throw new NotSupportedException("Use 'UploadAsync' methods in this class");
             case XHttpMethod.HEAD:
                 using (var message = await httpClient.GetAsync(url, HttpCompletionOption.ResponseContentRead))
                 {
                     if (message.StatusCode == HttpStatusCode.OK)
                         result = await message.Content.ReadAsStringAsync();
                 }
                 break;
         }
     }
     return result;
 }
示例#3
0
        internal async Task<string> ExecRequestAsync(string endpoint, XHttpMethod httpMethod, string httpData = null,bool encode = true, byte[] fileBytes = null, string fieldName = null, string fileName = null)
        {
            string result = string.Empty;
            if (encode && String.IsNullOrEmpty(apiKey))
                throw new FxApiException("User DFXKey not set");
            var dataCode = !String.IsNullOrEmpty(httpData) && encode ? _cryptProvider.Encrypt(httpData) : httpData; //

            var urlFull = endpoint != string.Empty ? apiUrl + endpoint : apiUrl;
            if (httpMethod != XHttpMethod.UPLOADDATA)
                result = await _requestProvider.SendAsync(urlFull, httpMethod, dataCode).ConfigureAwait(false);
            else
                result = await _requestProvider.UploadAsync(urlFull, dataCode, fileBytes, fieldName, fileName).ConfigureAwait(false);

            CheckResultRequest(result);
            var trimResult = encode ? _cryptProvider.Decrypt(result) : result;
            return trimResult.TrimEnd(new char[] { (char)0x1, (char)0x2, (char)0x3, (char)0x4, (char)0x5, (char)0x6, (char)0x8, (char)0x9, ' ', '\r', '\n', '\a' });
        }
示例#4
0
 internal string ExecRequest(string endpoint, XHttpMethod httpMethod, string httpData = null, bool encode = true, byte[] fileBytes = null, string fieldName = null, string fileName = null)
 {
     return ExecRequestAsync(endpoint, httpMethod, httpData, encode, fileBytes, fieldName, fileName).ConfigureAwait(false).GetAwaiter().GetResult();
 }