windows phone 8.1 HttpWebRequest 请求服务器

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 请求服务器

上一篇:阿里云服务器使用体验——希望大家和我一样有所收获


下一篇:windows8.1安装