jquery ajax跨域拿到接口数据

   

    接口

   

   

获取接口的数据

   


jquery ajax跨域拿到接口数据_第1张图片

但是有些网站是不可以跨域拿到的

此时,我们就要借助后台

新建一个类文件:

HttpClient.cs


using System;

using System.Collections.Generic;

using System.IO;

using System.IO.Compression;

using System.Linq;

using System.Net;

using System.Text;

namespace 接口

{

    public class HttpClient

    {

        ///

        /// post请求

        ///

        public static string Post(string url, string postDataStr)

        {

            string result = "";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

            request.CookieContainer = new CookieContainer();

            //以下是发送的http头,随便加,其中referer挺重要的,有些网站会根据这个来反盗链 

            request.Referer = url;

            request.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

            request.Headers["Accept-Language"] = "zh-CN,zh;q=0.";

            request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";

            request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";

            request.KeepAlive = true;

            //上面的http头看情况而定,但是下面俩必须加 

            request.ContentType = "application/x-www-form-urlencoded";

            request.Method = "POST";

            Encoding encoding = Encoding.UTF8;//根据网站的编码自定义 

            byte[] postData = encoding.GetBytes(postDataStr);//postDataStr即为发送的数据,格式还是和上次说的一样 

            request.ContentLength = postData.Length;

            Stream requestStream = request.GetRequestStream();

            requestStream.Write(postData, 0, postData.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();

            //如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可 

            if (response.Headers["Content-Encoding"] != null && response.Headers["Content-Encoding"].ToLower().Contains("gzip"))

            {

                responseStream = new GZipStream(responseStream, CompressionMode.Decompress);

            }

            StreamReader streamReader = new StreamReader(responseStream, encoding);

            string retString = streamReader.ReadToEnd();

            streamReader.Close();

            responseStream.Close();

            result = retString;

            return result;

        }

        ///

        /// get请求

        ///

        public static string Get(string url)

        {

            WebClient wc = new WebClient();

            wc.Encoding = Encoding.UTF8;

            return wc.DownloadString(url);

        }

        ///

        /// 下载文件

        ///

        public static int DownloadFile(string downloadurl, string saveurl)

        {

            WebClient web = new WebClient();

            web.DownloadFile(downloadurl, saveurl);

            return 1;

        }

    }

}

你可能感兴趣的:(jquery ajax跨域拿到接口数据)