跨域请求——WebClient通过get和post请求api

AJAX不可以实现跨域请求,经过特殊处理才行。一般后台可以通过WebClient实现跨域请求~

//get 请求
        string url = string.Format("http://localhost:28450/api/values?str1=a&str2=b");
        WebClient wc = new WebClient();
        Encoding enc = Encoding.GetEncoding("UTF-8");
        Byte[] pageData = wc.DownloadData(url);
        string re = enc.GetString(pageData);

//post 请求
        string postData = "value=a";
        byte[] bytes = Encoding.UTF8.GetBytes(postData);
        WebClient client = new WebClient();
        client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
        client.Headers.Add("ContentLength", postData.Length.ToString());
        Encoding enc = Encoding.GetEncoding("UTF-8");
        byte[] responseData = client.UploadData("http://localhost:28450/api/values", "POST", bytes);
        string re = enc.GetString(responseData);

来源:http://blog.csdn.net/zhaoqi5705/article/details/14920667

上一篇:MongoDB的基本操作


下一篇:css 继承性和层叠性