Esempio n. 1
0
        public static void DoDownload(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout, ref Stream stream)
        {
            HttpWebRequest webRequest = null;

            try
            {
                string queryString = paramers.GetQueryString();
                string getURL      = BuildGetUrl(url, queryString);

                webRequest = HttpConnection.GetRequest(getURL, HttpMethod.GET, header);
                webRequest.ReadWriteTimeout = readTimeout;
                webRequest.Timeout          = connectTimeout;
                GetResponseAsOutputStream(webRequest, ref stream);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 2
0
        public static string DoGet(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout)
        {
            HttpWebRequest webRequest = null;

            try
            {
                string queryString = paramers.GetQueryString();
                string getURL      = BuildGetUrl(url, queryString);

                webRequest = HttpConnection.GetRequest(getURL, HttpMethod.GET, header);
                webRequest.ReadWriteTimeout = readTimeout;
                webRequest.Timeout          = connectTimeout;

                return(GetResponseAsString(webRequest));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Esempio n. 3
0
        public static string DoPost(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout)
        {
            System.IO.Stream stream = null;

            HttpWebRequest webRequest = null;

            try
            {
                webRequest = HttpConnection.GetRequest(url, HttpMethod.POST, header);

                webRequest.ReadWriteTimeout = readTimeout;
                webRequest.Timeout          = connectTimeout;
                if (paramers.IsMultipart())
                {
                    string boundary = "----sdkboundary" + StringUtils.Random(6); // 随机分隔线
                    webRequest.ContentType = CONTENT_MULTIPART + boundary;

                    Stream memStream = new MemoryStream();

                    WriteMutiContent(boundary, paramers, ref memStream);

                    webRequest.ContentLength = memStream.Length;

                    Stream requestStream = webRequest.GetRequestStream();

                    memStream.Position = 0;
                    byte[] tempBuffer = new byte[memStream.Length];
                    memStream.Read(tempBuffer, 0, tempBuffer.Length);
                    memStream.Close();

                    requestStream.Write(tempBuffer, 0, tempBuffer.Length);
                    requestStream.Close();
                }
                else if (paramers.IsJsonApplication())
                {
                    string jsonString = paramers.JsonParamer;
                    byte[] jsonBytes  = System.Text.Encoding.UTF8.GetBytes(jsonString);
                    webRequest.ContentType = JSON_CONTENT_FORM;
                    stream = webRequest.GetRequestStream();
                    stream.Write(jsonBytes, 0, jsonBytes.Length);
                    stream.Close();
                }
                else
                {
                    string queryString = paramers.GetQueryString();
                    if (!String.IsNullOrEmpty(queryString))
                    {
                        byte[] queryBytes = System.Text.Encoding.UTF8.GetBytes(queryString);
                        webRequest.ContentType = CONTENT_FORM;
                        stream = webRequest.GetRequestStream();
                        stream.Write(queryBytes, 0, queryBytes.Length);
                        stream.Close();
                    }
                }

                return(GetResponseAsString(webRequest));
            }
            catch (Exception e)
            {
                if (stream != null)
                {
                    stream.Close();
                }
                throw e;
            }
        }