C# 模拟提交Get请求

 /// <summary>
        /// 模拟Get
        /// </summary>
        /// <param name="url">请求的url</param>
        /// <param name="content">请求的内容</param>
        /// <param name="headers">header里加入的内容</param>
        /// <param name="encode">编码,如果传null,默认是的是UTF8编码</param>
        /// <returns>请求返回的信息</returns>
        public string Get(string url, NameValueCollection content, NameValueCollection headers, Encoding encode)
        {
            string remoteInfo;

            var webClientObj = new WebClient { Encoding = encode ?? Encoding.UTF8 };
            webClientObj.Headers.Add(headers);

            try
            {
                webClientObj.QueryString.Add(content);
                return webClientObj.DownloadString(url);
            }
            catch (Exception ex)
            {
                remoteInfo = ex.ToString();
            }
            return remoteInfo;
        }


调用示例:

 string getURL = string.Format("http://{0}/services/sessions/{1}", this.PASSPORT_SERVER, token);

            NameValueCollection content = new NameValueCollection();
            content.Add("X-LITB-Correlation-ID", this.ACCESS_KEY+System.Guid.NewGuid().ToString());
            //content.Add("Authorization", GetAuthToken(this.ACCESS_KEY, this.ACCESS_PASSWORD));

            NameValueCollection head = new NameValueCollection();
            head.Add("ContentType", "text/xml; charset=UTF-8");
            head.Add("Accept", "application/xml");
            head.Add("MediaType", "application/xml");
            head.Add("Authorization", GetAuthToken(this.ACCESS_KEY, this.ACCESS_PASSWORD));
            string userInfoXML = Get(getURL, content, head, null);

感谢:Tea杨 提供的方法!

没有更多推荐了,返回首页