1、get请求类型接口调用
public JObject GetDate(string url)
{
url = RequestIP + url;//比如【http://192.168.xx.xx:1234】
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Accept", "application/json");//设置请求头
//get
var url1 = new Uri(url);
// response
var response = httpClient.GetAsync(url1).Result;
var data = response.Content.ReadAsStringAsync().Result;
JObject obj = (JObject)JsonConvert.DeserializeObject(data);
return obj;
}
}
处理接口返回的jobject数据
//数据结构
{\"message\": \"请求成功\",\"nid\": \"321\",\"nid\":{\"mid\": \"123456\",\"nid\": \"321\",\"details\": [" +
"{\"mid\": \"1\",\"name\": \"111\" }," +
"{\"mid\": \"5\", \"name\": \"555\" } ]" +
"}"+
"}";
//获取返回消息
JObject obj=GetDate(url)
obj["message"].ToString() == "请求成功"
//获取数据详情,
JArray jar = (JArray)JsonConvert.DeserializeObject(obj["details"].ToString());
string mid1=jar[0]["mid"].ToString()
string mid2=jar[1]["mid"].ToString()
2、 post请求类型接口调用
//content为body中的参数
// content数据格式:
content = "{\"identity_card_id\":\"" + identity.Content + "\"" +
",\"flight_no\":\"" + flight.Content.ToString() + "\",\"plane_ticket_no\":\"3790C40WILUZWU\",\"boarding_date\":\"" + this.boarding_date.Content.ToString() +
"\",\"seat_number\":\"" + seat_number.Content.ToString() + "\",\"aircraft_cabin\":\"" + aircraft_cabin.Content.ToString() + "\"" +
",\"checkcollecting\":\""+ userimage + "\",\"SecurityStates\":\"0\",\"process_node\":\"" + Constant.PROCESS_NODE_SIC_CODE + "\"}";
public string PostDate(string url, string content)
{
url = RequestIP + url;//比如【http://192.168.xx.xx:1234】
//post
HttpWebRequest httpWeb = (HttpWebRequest)WebRequest.Create(url);
httpWeb.Headers.Add("Accept", "application/json");//添加消息头
httpWeb.Timeout = 20000;
httpWeb.Method = "POST";
httpWeb.ContentType = "application/json; charset=utf-8";
byte[] bytePara = Encoding.UTF8.GetBytes(content);
using (Stream reqStream = httpWeb.GetRequestStream())
{
//提交数据
reqStream.Write(bytePara, 0, bytePara.Length);
}
//获取服务器返回值
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWeb.GetResponse();
Stream stream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
//获得返回值
string result = streamReader.ReadToEnd();
stream.Close();
return result;
}
处理接口返回值
var jObject1 = JObject.Parse(PostDate(url, content));
if(jObject1["message"].ToString() == "请求成功")
有兴趣的小伙伴可以关注我的公众号哦~ 里面有很多自己平时积攒起来的学习资源,Python,web安全,前端,Java等,都是免费领取的