示例#1
0
 public static void AddAuthorizationHeader(WebRequest request, AuthHeaderGenerator authHeaderGen)
 {
     if (authHeaderGen != null)
     {
         request.Headers.Add(authHeaderGen.Name, authHeaderGen.Value);
     }
 }
示例#2
0
 private static void SetupAuthorization(WebRequest request, AuthHeaderGenerator authHeaderGen)
 {
     if (typeof(ApiTokenAuthHeaderGenerator).IsInstanceOfType(authHeaderGen))
     {
         SetupAuthorization(request, authHeaderGen.Value);
     } else if (typeof(SessionIdAuthHeaderGenerator).IsInstanceOfType(authHeaderGen))
     {
         request.Headers.Add(authHeaderGen.Name, authHeaderGen.Value);
     }
 }
        public static byte[] PostHttp(AuthHeaderGenerator authHeaderGen, string path, byte[] content, IDictionary <string, string> headers)
        {
            try
            {
                System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path);
                request.Method        = "POST";
                request.ContentType   = ESL_CONTENT_TYPE_APPLICATION_JSON;
                request.ContentLength = content.Length;
                AddAdditionalHeaders(request, headers);
                setupAuthorization(request, authHeaderGen.Value);
                request.Accept = ESL_ACCEPT_TYPE_APPLICATION_JSON;
                SetProxy(request);

                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(content, 0, content.Length);
                }

                WebResponse response = request.GetResponse();

                using (Stream responseStream = response.GetResponseStream())
                {
                    var memoryStream = new MemoryStream();
                    CopyTo(responseStream, memoryStream);

                    byte[] result = memoryStream.ToArray();
                    return(result);
                }
            }
            catch (WebException e)
            {
                using (var stream = e.Response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        string errorDetails = reader.ReadToEnd();
                        throw new EslServerException(String.Format("{0} HTTP {1} on URI {2}. Optional details: {3}", e.Message,
                                                                   ((HttpWebResponse)e.Response).Method, e.Response.ResponseUri, errorDetails),
                                                     errorDetails, e);
                    }
            }
            catch (Exception e)
            {
                throw new EslException("Error communicating with esl server. " + e.Message, e);
            }
        }
示例#4
0
        public static string MultipartPostHttp(string apiToken, string path, byte[] content, string boundary, AuthHeaderGenerator authHeaderGen)
        {
            WebRequest request = WebRequest.Create(path);

            try
            {
                System.Net.ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;

                request.Method        = "POST";
                request.ContentType   = string.Format(ESL_CONTENT_TYPE_APPLICATION_MULTIPART, boundary);
                request.ContentLength = content.Length;
                AddAuthorizationHeader(request, authHeaderGen);
                SetProxy(request);

                using (Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(content, 0, content.Length);
                }

                WebResponse response = request.GetResponse();

                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                    return(reader.ReadToEnd());
                }
            }
            catch (WebException e)
            {
                using (var stream = e.Response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        string errorDetails = reader.ReadToEnd();
                        throw new EslServerException(String.Format("{0} HTTP {1} on URI {2}. Optional details: {3}", e.Message,
                                                                   ((HttpWebResponse)e.Response).Method, e.Response.ResponseUri, errorDetails),
                                                     errorDetails, e);
                    }
            }
            catch (Exception e)
            {
                throw new EslException("Error communicating with esl server. " + e.Message, e);
            }
        }
示例#5
0
 public static string MultipartPostHttp(string apiKey, string path, byte[] content, string boundary, AuthHeaderGenerator authHeaderGen)
 {
     return(MultipartPostHttp(apiKey, path, content, boundary, authHeaderGen, new Dictionary <string, string> ()));
 }
示例#6
0
 public static byte [] DeleteHttp(AuthHeaderGenerator authHeader, string path, byte [] content, IDictionary <string, string> headers)
 {
     headers[authHeader.Name] = authHeader.Value;
     return(DeleteHttp(path, content, headers, (string)null));
 }
示例#7
0
 public static byte[] PostHttp(AuthHeaderGenerator authHeaderGen, string path, byte[] content)
 {
     return(PostHttp(authHeaderGen, path, content, new Dictionary <string, string> ()));
 }
示例#8
0
		 public static byte[] MultipartPostHttp (string apiToken, string path, byte[] content, string boundary, AuthHeaderGenerator authHeaderGen)
		{
			WebRequest request = WebRequest.Create (path);
			try {
				request.Method = "POST";
				request.ContentType = string.Format ("multipart/form-data; boundary={0}", boundary);
				request.ContentLength = content.Length;
				AddAuthorizationHeader(request, authHeaderGen);

				using (Stream dataStream = request.GetRequestStream ()) {
					dataStream.Write (content, 0, content.Length);
				}

				WebResponse response = request.GetResponse ();

				using (Stream responseStream = response.GetResponseStream()) {
					var memoryStream = new MemoryStream ();
					CopyTo (responseStream, memoryStream);
					byte[] result = memoryStream.ToArray();

					return result;
				}
			}
            catch (WebException e){
                using (var stream = e.Response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    string errorDetails = reader.ReadToEnd();
                    throw new EslServerException(String.Format("{0} HTTP {1} on URI {2}. Optional details: {3}", e.Message, 
                                                               ((HttpWebResponse)e.Response).Method, e.Response.ResponseUri, errorDetails),
                                                                errorDetails, e);
                }
            }
			catch (Exception e) {
				throw new EslException("Error communicating with esl server. " + e.Message,e);
			}
		}
示例#9
0
		public static void AddAuthorizationHeader(WebRequest request, AuthHeaderGenerator authHeaderGen)
		{
			request.Headers.Add(authHeaderGen.Name, authHeaderGen.Value);
		}
示例#10
0
        public static byte[] MultipartPostHttp(string apiToken, string path, byte[] content, string boundary, AuthHeaderGenerator authHeaderGen)
        {
            WebRequest request = WebRequest.Create(path);

            try {
                request.Method        = "POST";
                request.ContentType   = string.Format("multipart/form-data; boundary={0}", boundary);
                request.ContentLength = content.Length;
                AddAuthorizationHeader(request, authHeaderGen);

                using (Stream dataStream = request.GetRequestStream()) {
                    dataStream.Write(content, 0, content.Length);
                }

                WebResponse response = request.GetResponse();

                using (Stream responseStream = response.GetResponseStream()) {
                    var memoryStream = new MemoryStream();
                    CopyTo(responseStream, memoryStream);
                    byte[] result = memoryStream.ToArray();

                    return(result);
                }
            }
            catch (WebException e) {
                using (var stream = e.Response.GetResponseStream())
                    using (var reader = new StreamReader(stream))
                    {
                        string errorDetails = reader.ReadToEnd();
                        throw new EslServerException(String.Format("{0} HTTP {1} on URI {2}. Optional details: {3}", e.Message,
                                                                   ((HttpWebResponse)e.Response).Method, e.Response.ResponseUri, errorDetails),
                                                     errorDetails, e);
                    }
            }
            catch (Exception e) {
                throw new EslException("Error communicating with esl server. " + e.Message, e);
            }
        }