Example #1
0
        public static string DoPost(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout)
        {
            Stream         requestStream = null;
            HttpWebRequest request       = null;
            string         responseAsString;

            try
            {
                request = HttpConnection.GetRequest(url, HttpMethod.POST, header);
                request.ReadWriteTimeout = readTimeout;
                request.Timeout          = connectTimeout;
                if (paramers.IsMultipart())
                {
                    string boundary = "----sdkboundary" + StringUtils.Random(6);
                    request.ContentType = CONTENT_MULTIPART + boundary;
                    Stream stream2 = new MemoryStream();
                    WriteMutiContent(boundary, paramers, ref stream2);
                    request.ContentLength = stream2.Length;
                    stream2.Position      = 0L;
                    byte[] buffer = new byte[stream2.Length];
                    stream2.Read(buffer, 0, buffer.Length);
                    stream2.Close();
                    requestStream = request.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
                    requestStream.Close();
                }
                else if (paramers.IsJsonApplication())
                {
                    string jsonParamer = paramers.JsonParamer;
                    byte[] bytes       = Encoding.UTF8.GetBytes(jsonParamer);
                    request.ContentType = JSON_CONTENT_FORM;
                    requestStream       = request.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Close();
                }
                else
                {
                    string queryString = paramers.GetQueryString();
                    if (!string.IsNullOrEmpty(queryString))
                    {
                        byte[] bytes = Encoding.UTF8.GetBytes(queryString);
                        request.ContentType = CONTENT_FORM;
                        requestStream       = request.GetRequestStream();
                        requestStream.Write(bytes, 0, bytes.Length);
                        requestStream.Close();
                    }
                }
                responseAsString = GetResponseAsString(request);
            }
            catch (Exception exception1)
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
                throw exception1;
            }
            return(responseAsString);
        }
Example #2
0
 private void PolyfixOfOldApi(ref HttpParamers paramers)
 {
     // old sxqian api polly fill
     paramers.AddParamer("yclDataStore.appKey", this.AccessToken);
     paramers.AddParamer("yclDataStore.appSecret", this.AccessSecret);
     if (!string.IsNullOrEmpty(this.callBackUrl))
     {
         paramers.AddParamer("yclDataStore.callBackUrl", this.callBackUrl);
     }
 }
Example #3
0
 public static void DoDownload(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout, ref Stream stream)
 {
     try
     {
         string         queryString = paramers.GetQueryString();
         HttpWebRequest request     = HttpConnection.GetRequest(BuildGetUrl(url, queryString), HttpMethod.GET, header);
         request.ReadWriteTimeout = readTimeout;
         request.Timeout          = connectTimeout;
         GetResponseAsOutputStream(request, ref stream);
     }
     catch (Exception exception1)
     {
         throw exception1;
     }
 }
Example #4
0
        public static string DoGet(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout)
        {
            string responseAsString;

            try
            {
                string         queryString = paramers.GetQueryString();
                HttpWebRequest request     = HttpConnection.GetRequest(BuildGetUrl(url, queryString), HttpMethod.GET, header);
                request.ReadWriteTimeout = readTimeout;
                request.Timeout          = connectTimeout;
                responseAsString         = GetResponseAsString(request);
            }
            catch (Exception exception1)
            {
                throw exception1;
            }
            return(responseAsString);
        }
Example #5
0
 private static void WriteMutiContent(string boundary, HttpParamers paramers, ref Stream stream)
 {
     byte[] bytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
     foreach (KeyValuePair <string, string> pair in paramers.Paramers)
     {
         byte[] textEntry = GetTextEntry(pair.Key, pair.Value);
         stream.Write(bytes, 0, bytes.Length);
         stream.Write(textEntry, 0, textEntry.Length);
     }
     if (paramers.FileStreams.Count > 0)
     {
         foreach (KeyValuePair <string, IFileItem> pair2 in paramers.FileStreams)
         {
             IFileItem item = pair2.Value;
             if (!item.IsValid())
             {
                 throw new Exception("无效的文件流");
             }
             byte[] buffer4 = GetFileEntry(pair2.Key, item.GetFileName(), item.GetMimeType());
             stream.Write(bytes, 0, bytes.Length);
             stream.Write(buffer4, 0, buffer4.Length);
             item.Write(ref stream);
         }
     }
     if (paramers.Files.Count > 0)
     {
         foreach (KeyValuePair <string, List <IFileItem> > pair3 in paramers.Files)
         {
             foreach (IFileItem item2 in pair3.Value)
             {
                 if (!item2.IsValid())
                 {
                     throw new Exception("无效的文件流");
                 }
                 byte[] buffer5 = GetFileEntry(pair3.Key, item2.GetFileName(), item2.GetMimeType());
                 stream.Write(bytes, 0, bytes.Length);
                 stream.Write(buffer5, 0, buffer5.Length);
                 item2.Write(ref stream);
             }
         }
     }
     byte[] buffer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
     stream.Write(buffer, 0, buffer.Length);
 }
Example #6
0
        public void HttpDownload(string serviceUrl, HttpParamers paramers, ref Stream outputStream)
        {
            string url = this.ServerUrl + serviceUrl;
            double totalMilliseconds = DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds;

            byte[]     inBytes   = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(this.AccessToken + this.AccessSecret + totalMilliseconds.ToString()));
            string     signature = StringUtils.ByteToString(inBytes, inBytes.Length);
            HttpHeader header    = new HttpHeader(this.AccessToken, this.AccessSecret, totalMilliseconds, signature, this.VERSION);

            PolyfixOfOldDownloadApi(ref paramers);
            try
            {
                HttpClient.DoDownload(url, paramers, header, this.connectTimeout, this.readTimeout, ref outputStream);
            }
            catch (Exception exception1)
            {
                throw new Exception(exception1.Message);
            }
        }
Example #7
0
        public static string DoService(string url, HttpParamers paramers, HttpHeader header, int connectTimeout, int readTimeout)
        {
            string     str;
            HttpMethod method = paramers.Method;

            try
            {
                if (method != HttpMethod.GET)
                {
                    if (method != HttpMethod.POST)
                    {
                        throw new Exception("不支持的HTTP方法类型");
                    }
                    return(DoPost(url, paramers, header, connectTimeout, readTimeout));
                }
                str = DoGet(url, paramers, header, connectTimeout, readTimeout);
            }
            catch (Exception exception1)
            {
                throw exception1;
            }
            return(str);
        }
Example #8
0
 private void PolyfixOfOldDownloadApi(ref HttpParamers paramers)
 {
     // old sxqian api polly fill
     paramers.AddParamer("appKey", this.AccessToken);
     paramers.AddParamer("appSecret", this.AccessSecret);
 }