码迷,mamicode.com
首页 > Windows程序 > 详细

windows phone 8.1 HttpWebRequest 请求服务器

时间:2014-11-07 13:03:18      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   ar   os   sp   div   on   

public  string SendGetRequest(string baseurl, string parameters)
        {
            string parassb = parameters;
            
            if (parassb.Length > 0)
            {
                baseurl += "?" + parassb;
            }
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(baseurl);
            req.Method = "GET";
            req.ContinueTimeout = 1000;

            string result = String.Empty;
            using (StreamReader reader = new StreamReader(req.GetResponseAsync().Result.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }

上面代码是GET方式请求;

public async Task<string> PostRequest(string url, string parameters)
        {
            var request = HttpWebRequest.Create(url);
            request.Method = "POST";
            using (Stream result = await request.GetRequestStreamAsync())
            {
                byte[] bytes = Encoding.UTF8.GetBytes(parameters);
                result.Write(bytes, 0, bytes.Length);
            }
            var response = await request.GetResponseAsync();
            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    string content = reader.ReadToEnd();

                    return content;
                }
            }
            return null;
        }

这是POST请求方式

windows phone 8.1 HttpWebRequest 请求服务器

标签:style   blog   http   color   ar   os   sp   div   on   

原文地址:http://www.cnblogs.com/shi-meng/p/4080848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!