コード例 #1
0
 public static bool IsNullOrEmpty(this QueryStringBuilder Builder)
 {
     if (Builder == null)
     {
         return(true);
     }
     else if (Builder.IsEmpty() == true)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #2
0
        public static string BuildEndPoint(
            string Url, string WebServiceType, QueryStringBuilder QueryString)
        {
            StringBuilder sb = new StringBuilder();

            if (Url.IsNullOrEmpty())
            {
                throw new ApplicationException("URL is empty");
            }

            // add the url to the builder.
            sb.Append(Url);

            // add servicetype
            if (WebServiceType.IsNullOrEmpty() == false)
            {
                if (sb.EndsWith("/"))
                {
                    sb.Length = sb.Length - 1;
                }

                if (WebServiceType[0] != '/')
                {
                    sb.Append('/');
                }

                sb.Append(WebServiceType);
            }

            // QueryString
            if (QueryString.IsNullOrEmpty() == false)
            {
                sb.Append('?' + QueryString.ToString( ));
            }

            return(sb.ToString());
        }
コード例 #3
0
        /// <summary>
        /// Send web request to server using the POST method.
        /// </summary>
        /// <param name="Url"></param>
        /// <param name="WebServiceType"></param>
        /// <param name="QueryString"></param>
        /// <param name="ContentData"></param>
        /// <param name="UserAgent"></param>
        /// <param name="ContentType"></param>
        /// <param name="Timeout"></param>
        /// <returns></returns>
        public static string SendWebRequest_Post(
            string Url, string WebServiceType, QueryStringBuilder QueryString,
            byte[] ContentData,
            string UserAgent   = null,
            string ContentType = "application/x-www-form-urlencoded; charset=utf-8",
            IDictionary <string, string> HeaderMap = null,
            Stream SendData = null,
            int Timeout     = 50000)
        {
            string endPoint = BuildEndPoint(Url, WebServiceType, QueryString);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);

            request.Timeout     = Timeout;
            request.UserAgent   = UserAgent;
            request.Method      = "POST";
            request.ContentType = ContentType;

            if (ContentData != null)
            {
                request.ContentLength = ContentData.Length;
            }

            // add headers
            if (HeaderMap != null)
            {
                WebHeaderCollection headers = request.Headers;
                foreach (String key in HeaderMap.Keys)
                {
                    headers.Add(key, HeaderMap[key]);
                }
            }

            // fill the WebRequest content stream.
            if (ContentData != null)
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(ContentData, 0, ContentData.Length);
                    requestStream.Close();
                }
            }

            // copy the SendData into the web request.
            if (SendData != null)
            {
                using (Stream requestStream = request.GetRequestStream())
                {
                    SendData.Position = 0;
                    CopyStream(SendData, requestStream);
                    requestStream.Close();
                }
            }

            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    string respText = null;

                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        string message = String.Format(
                            "POST failed. Received HTTP {0}", response.StatusCode);
                        throw new ApplicationException(message);
                    }

                    // read the response
                    using (var responseStream = response.GetResponseStream())
                    {
                        using (var reader = new StreamReader(responseStream))
                        {
                            respText = reader.ReadToEnd();
                        }
                    }

                    return(respText);
                }
            }
            catch (WebException excp)
            {
                throw excp;
            }
        }