<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>接口</title>
<script src="jquery-1.11.1.min.js"></script>
</head>
<body>
<h6>获取接口的数据</h6>
<script>
$(function () {
//跨域求接口的数据
$.ajax({
url: "http://www.jxntv.cn/data/jmd-jxtv2.html",
//有些网站是可以通过这种方式跨域来获取数据的
dataType: 'jsonp',
jsonpCallback: 'list',
type: 'post',
// 下面的两行代码,就是解决跨域的关键
xhrFields: { withCredentials: true },
crossDomain: true,
success: function (data)
{
console.log(data);
},
error: function (responseText, textStatus, XMLHttpRequest) {
alert(textStatus);
}
});
})
</script>
</body>
</html>
但是有些网站是不可以跨域拿到的
此时,我们就要借助后台
<script>
$(function () {
//跨域求接口的数据
$.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>
新建一个类文件:
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请求
/// </summary>
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;
}
/// <summary>
/// get请求
/// </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;
}
}
}
网友评论