コード例 #1
0
ファイル: WebUtils.cs プロジェクト: sd009896/CustomerMall
        /// <summary>
        /// 执行HTTP GET请求。
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="parameters">请求参数</param>
        /// <returns>HTTP响应</returns>
        public string DoGet(string url, IDictionary <string, string> parameters)
        {
            if (parameters != null && parameters.Count > 0)
            {
                if (url.Contains("?"))
                {
                    url = url + "&" + BuildQuery(parameters);
                }
                else
                {
                    url = url + "?" + BuildQuery(parameters);
                }
            }

            HttpWebRequest req = GetWebRequest(url, "GET");

            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();

            if (WebUtils.IsNullOrWhiteSpace(rsp.CharacterSet))
            {
                return(GetResponseAsString(rsp, Encoding.UTF8));
            }
            else
            {
                Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
                return(GetResponseAsString(rsp, encoding));
            }
        }
コード例 #2
0
        /// <summary>
        /// 获取Json string某节点的值。
        /// </summary>
        /// <param name="json"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetJosnValue(string jsonStr, string key)
        {
            string result = string.Empty;

            if (!WebUtils.IsNullOrWhiteSpace(jsonStr))
            {
                key = "\"" + key.Trim('"') + "\"";
                int index = jsonStr.IndexOf(key) + key.Length + 1;
                if (index > key.Length + 1)
                {
                    //先截逗号,若是最后一个,截“}”号,取最小值

                    int end = jsonStr.IndexOf(',', index);
                    if (end == -1)
                    {
                        end = jsonStr.IndexOf('}', index);
                    }
                    //index = json.IndexOf('"', index + key.Length + 1) + 1;
                    result = jsonStr.Substring(index, end - index);
                    //过滤引号或空格
                    result = result.Trim(new char[] { '"', ' ', '\'' });
                }
            }
            return(result);
        }
コード例 #3
0
ファイル: WebUtils.cs プロジェクト: sd009896/CustomerMall
        /// <summary>
        /// 执行HTTP POST请求。
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="parameters">请求参数</param>
        /// <returns>HTTP响应</returns>
        public string DoPost(string url, IDictionary <string, string> parameters)
        {
            HttpWebRequest req = GetWebRequest(url, "POST");

            req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            byte[]           postData  = Encoding.UTF8.GetBytes(BuildQuery(parameters));
            System.IO.Stream reqStream = req.GetRequestStream();
            reqStream.Write(postData, 0, postData.Length);
            reqStream.Close();

            HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();

            if (WebUtils.IsNullOrWhiteSpace(rsp.CharacterSet))
            {
                return(GetResponseAsString(rsp, Encoding.UTF8));
            }
            else
            {
                Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
                return(GetResponseAsString(rsp, encoding));
            }
        }
コード例 #4
0
ファイル: WebUtils.cs プロジェクト: sd009896/CustomerMall
        public bool DownloadFile(string url, string thumb_media_id, string saveDir, out string saveFileName, out string errHtml)
        {
            if (url.IndexOf("showqrcode") != -1)
            {
                return(DownloadQrCode(url, saveDir, out saveFileName, out errHtml));
            }

            saveFileName = string.Empty;
            errHtml      = string.Empty;
            bool            isSuc    = false;
            HttpWebResponse response = null;
            HttpWebRequest  request  = null;

            try
            {
                request = GetWebRequest(url, "GET");
                // if (url.IndexOf("showqrcode") != -1)
                //{
                //    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0";
                //}

                response = (HttpWebResponse)request.GetResponse();
                if (!response.ContentType.ToLower().StartsWith("text/"))
                {
                    string disp = response.Headers.Get("Content-type");
                    string ext  = "." + disp.Substring(disp.LastIndexOf("/") + 1);
                    saveFileName = saveDir;
                    if (!saveFileName.EndsWith("\\"))
                    {
                        saveFileName += "\\";
                    }

                    saveFileName = thumb_media_id + ext;
                    string saveName = saveDir + saveFileName;
                    SaveBinaryFile(response, saveName);
                    isSuc = true;
                }
                else
                {
                    if (WebUtils.IsNullOrWhiteSpace(response.CharacterSet))
                    {
                        errHtml = GetResponseAsString(response, Encoding.UTF8);
                    }
                    else
                    {
                        Encoding encoding = Encoding.GetEncoding(response.CharacterSet);
                        errHtml = GetResponseAsString(response, encoding);
                    }
                    isSuc = false;
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request = null;
                }
            }


            return(isSuc);
        }