<!DOCTYPE html>
<html>
<head>
? ? <meta charset="utf-8" />
? ? <title>接口</title>
? ? <script src="jquery-1.11.1.min.js"></script>
</head>
<body>
? ? <h6>獲取接口的數(shù)據(jù)</h6>
? ? <script>
? ? ? ? $(function () {
? ? ? ? ? ? //跨域求接口的數(shù)據(jù)
? ? ? ? ? ? $.ajax({
? ? ? ? ? ? ? ? url: "http://www.jxntv.cn/data/jmd-jxtv2.html",
? ? ? ? ? ? ? ? //有些網(wǎng)站是可以通過(guò)這種方式跨域來(lái)獲取數(shù)據(jù)的
? ? ? ? ? ? ? ? dataType: 'jsonp',
? ? ? ? ? ? ? ? jsonpCallback: 'list',
? ? ? ? ? ? ? ? type: 'post',
? ? ? ? ? ? ? ? // 下面的兩行代碼,就是解決跨域的關(guān)鍵
? ? ? ? ? ? ? ? xhrFields: { withCredentials: true },
? ? ? ? ? ? ? ? crossDomain: true,
? ? ? ? ? ? ? ? success: function (data)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? console.log(data);
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? error: function (responseText, textStatus, XMLHttpRequest) {
? ? ? ? ? ? ? ? ? ? alert(textStatus);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? })
? ? </script>
</body>
</html>
但是有些網(wǎng)站是不可以跨域拿到的
此時(shí)钻洒,我們就要借助后臺(tái)
<script>
? ? ? ? $(function () {
? ? ? ? ? ? //跨域求接口的數(shù)據(jù)
? ? ? ? ? ? $.ajax({
? ? ? ? ? ? ? ? type: "get",
? ? ? ? ? ? ? ? dataType: "text",
? ? ? ? ? ? ? ? url: "default.ashx",
? ? ? ? ? ? ? ? data: "url=http://120.198.124.121:8087/openlib/service/statis/sync/36?token=683a7f9b0dbbbf3034613f8bc91cae76-dae696f54063b47c58e24323c8f11ff4&libcode=%E2%80%98ZT%E2%80%99&rownum=7&starttime=2019/4/10&endtime=2019/4/11",
? ? ? ? ? ? ? ? success: function (data)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? console.log(data);
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ? ? error: function (responseText, textStatus, XMLHttpRequest) {
? ? ? ? ? ? ? ? ? ? alert(textStatus);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? });
? ? ? ? })
? ? </script>
新建一個(gè)類(lèi)文件:
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
? ? {
? ? ? ? /// <summary>
? ? ? ? /// post請(qǐng)求
? ? ? ? /// </summary>
? ? ? ? public static string Post(string url, string postDataStr)
? ? ? ? {
? ? ? ? ? ? string result = "";
? ? ? ? ? ? HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
? ? ? ? ? ? request.CookieContainer = new CookieContainer();
? ? ? ? ? ? //以下是發(fā)送的http頭,隨便加锄开,其中referer挺重要的素标,有些網(wǎng)站會(huì)根據(jù)這個(gè)來(lái)反盜鏈?
? ? ? ? ? ? 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頭看情況而定,但是下面?zhèn)z必須加?
? ? ? ? ? ? request.ContentType = "application/x-www-form-urlencoded";
? ? ? ? ? ? request.Method = "POST";
? ? ? ? ? ? Encoding encoding = Encoding.UTF8;//根據(jù)網(wǎng)站的編碼自定義?
? ? ? ? ? ? byte[] postData = encoding.GetBytes(postDataStr);//postDataStr即為發(fā)送的數(shù)據(jù)萍悴,格式還是和上次說(shuō)的一樣?
? ? ? ? ? ? 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;
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// get請(qǐng)求
? ? ? ? /// </summary>
? ? ? ? public static string Get(string url)
? ? ? ? {
? ? ? ? ? ? WebClient wc = new WebClient();
? ? ? ? ? ? wc.Encoding = Encoding.UTF8;
? ? ? ? ? ? return wc.DownloadString(url);
? ? ? ? }
? ? ? ? /// <summary>
? ? ? ? /// 下載文件
? ? ? ? /// </summary>
? ? ? ? public static int DownloadFile(string downloadurl, string saveurl)
? ? ? ? {
? ? ? ? ? ? WebClient web = new WebClient();
? ? ? ? ? ? web.DownloadFile(downloadurl, saveurl);
? ? ? ? ? ? return 1;
? ? ? ? }
? ? }
}