Ajax 请求接口

Posted wusiraaron

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ajax 请求接口相关的知识,希望对你有一定的参考价值。

1.获取 Token
string url = "http://10.168.9.21/api/services/Org/UserLogin";
var para = new

UserName = "admin",
Password = "edoc2"
;
var result_NewIncident = HttpUtility.HttpPostJson(url, para);
JObject obj = JObject.Parse(result_NewIncident);
string token = obj.GetValue("data").ToString();

 

 

附件是 请求通用类  Post Get    通过 webclient

 

/// <summary>
/// HttpUtility
/// </summary>
public class HttpUtility

/// <summary>
/// HttpClient
/// </summary>
private static HttpClient client = new HttpClient(new HttpClientHandler() AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip );

/// <summary>
/// Content-Type:application/json
/// </summary>
/// <param name="apiUrl"></param>
/// <param name="data"></param>
/// <returns></returns>
public static string HttpPostJson(string apiUrl, Object data)

string result = string.Empty;


//client.DefaultRequestHeaders.Add("Authorization", $"Token.type Token.access_token");

var postData = JsonConvert.SerializeObject(data);

var httpContent = new StringContent(postData, Encoding.UTF8);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = client.PostAsync(apiUrl, httpContent);

response.Wait();

if (response.IsCompleted)

var stream = response.Result.Content.ReadAsStreamAsync();

stream.Wait();
using (var reader = new StreamReader(stream.Result))

result = reader.ReadToEnd();


return result;

/// <summary>
/// Content-Type:application/json
/// </summary>
/// <param name="apiUrl"></param>
/// <returns></returns>
public static string HttpGetJson(string apiUrl)

string result = string.Empty;


//client.DefaultRequestHeaders.Add("Authorization", $"Token.type Token.access_token");

var response = client.GetAsync(apiUrl);

response.Wait();

if (response.IsCompleted)

var stream = response.Result.Content.ReadAsStreamAsync();

stream.Wait();
using (var reader = new StreamReader(stream.Result))

result = reader.ReadToEnd();

 

return result;

/// <summary>
/// Content-Type:application/x-www-form-urlencoded
/// </summary>
/// <param name="apiUrl"></param>
/// <param name="dict"></param>
/// <returns></returns>
public static string HttpPostForm(string apiUrl, IDictionary<string, string> dict)

string result = string.Empty;


//client.DefaultRequestHeaders.Add("Authorization", $"Token.type Token.access_token");

var httpContent = new FormUrlEncodedContent(dict);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
var response = client.PostAsync(apiUrl, httpContent);

response.Wait();

if (response.IsCompleted)

var stream = response.Result.Content.ReadAsStreamAsync();

stream.Wait();
using (var reader = new StreamReader(stream.Result))

result = reader.ReadToEnd();

return result;

 

以上是关于Ajax 请求接口的主要内容,如果未能解决你的问题,请参考以下文章

ajax请求接口怎么获得返回数据

ajax请求接口里的数据,然后显示在页面里

ajax背景ajax对象ajax状态ajax与httpajax请求数据接口同步与异步ajax请求XML数据封装ajax函数artTemplate简介同源策略和跨域请求JSONP

ajax基础学习

使用js+Ajax请求API接口数据-带请求头方式

ajax请求接口数据