Asp.Net后台调用Http接口通用方法

1.通过WebClient类Post数据到远程地址,需要Basic认证:
        /// <summary>
        /// 通过WebClient类Post数据到远程地址,需要Basic认证;
        /// 调用端自己处理异常
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="paramStr">name=张三&age=20</param>
        /// <param name="encoding">请先确认目标网页的编码方式</param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string CallUrl(string uri, string paramStr, Encoding encoding, string username, string password)
        {
            if (encoding == null)
                encoding = Encoding.UTF8;
            string result = string.Empty;
            WebClient wc = new WebClient();
            // 采取POST方式必须加的Header
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            byte[] postData = encoding.GetBytes(paramStr);
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
            {
                CredentialCache credCache = new CredentialCache();
                credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
                wc.Credentials = credCache;
                string authorization = string.Format("{0}:{1}", username, password);
                wc.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization)));
            }
            byte[] responseData = wc.UploadData(uri, "POST", postData); // 得到返回字符流
            return encoding.GetString(responseData);// 解码                  
        }

2.需要在Header中添加参数传输
public static string CallUrl(string uri, string paramStr, Encoding encoding, Dictionary<string, string> dicHeader)
        {
            if (encoding == null)
                encoding = Encoding.UTF8;
            string result = string.Empty;
            WebClient wc = new WebClient();
            wc.Credentials = CredentialCache.DefaultCredentials;
            // 采取POST方式必须加的Header 
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            //wc.Headers.Add("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
            wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)");
            byte[] postData = encoding.GetBytes(paramStr);
            foreach (var item in dicHeader)
            {
                wc.Headers.Add(item.Key, item.Value);
            }
            byte[] responseData = wc.UploadData(uri, "POST", postData); // 得到返回字符流
            return encoding.GetString(responseData);// 解码                  
        }

转载请说明出处:第六感博客 原文链接:

标签: C# Asp.Net

相关阅读:

C#使用RSA非对称加密字符串 附Helper帮助类

asp.net WebPage WebMvc 压缩输出html

asp.net 内置委托 Action 和 Func 和 Predicate

vs生成成功但发布时报错 未能将文件...复制到... 未能找到文件...

FluentData 报错 Error:Could not map ...字段名