HTTP接口目前接触到的有两种:
①.共同点: 模式相同,接口方法大致相同, ②.不同点: 传参方式不同 HttpWebRequest中的ContentType方法不同 键值对方式为:“application/x-www-form-urlencoded” 非键值对为: “application/json”;
1.接口请求体为键值对方式
键值对请求体样例:
Srv中先定义:
private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
接口方法:
string url = "http://10.40.95.30/nwom/plugins/jswebservice/siteperformanceassess/assessResult.ilf"; --接口地址
string outData = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "post";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = DefaultUserAgent;
Encoding requestEncoding = Encoding.GetEncoding("UTF-8");
string repData = JsonConvert.SerializeObject(opDetailList); --opDetailList为请求体
StringBuilder buffer = new StringBuilder();
buffer.AppendFormat("{0}={1}", "opDetail", repData); --opDetail为键(按照他的要求定义键),repData值(解析之后的请求体)
byte[] postData = requestEncoding.GetBytes(buffer.ToString());
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(postData, 0, postData.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
using (Stream stream = resp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
outData = sr.ReadToEnd();
}
}
if (outData != null)
{
}
2.接口请求体非键值对
非键值对请求体样例: "{ " alarmtitle \ ": [“基站退服”,“小区退服”], \ "exResId \ ":[“2026413640”,“2031327029”], \ "alarmnetype \ ":[“BTS”,“NodeB”], \ "startEventTime\ ": 1542536234, \ "endEventTime\ ": 1542709034 }" 接方法口:
UserNum un = new UserNum();
un.userName = userName;
un.password = passWord;
string repData = JsonConvert.SerializeObject(un);
byte[] postData = Encoding.UTF8.GetBytes(repData);
string importProjectApiUrl = "http://10.40.9.185:60001/cloud/ailk-cms-ms-uac/auth";
string outData = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(importProjectApiUrl);
req.Method = "post";
req.ContentType = "application/json";
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(postData, 0, postData.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
using (Stream stream = resp.GetResponseStream())
{
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
outData = sr.ReadToEnd();
}
}
if (outData != null)
{
}
特殊实例: 接口方要求请求接口时带参数: 类似: curl -XPOST -H ‘Content-Type:application/json;charset=utf-8’ -H ‘Authorization:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJhY2Nfb2lkIjo0NzQsImFyZWFfbmFtZSI6ImFkbWluLmNvbSIsInN1YiI6ImNtcyIsInNwZWNpYWx0eSI6WzEsMiwzLDQsNSw2LDddLCJhY2NfbG9naW5uYW1lIjoiYWlsayIsImltc19waG9uZSI6IjE1MDYyMjA1NDk2IiwiYXJlYV9pZCI6MSwicm9sZV9uYW1lIjoiU1lTVEVNIiwiY2l0eV9uYW1lIjoi5rGf6IuP55yBIiwiY2l0eV9sYXllciI6MSwicm9sZV9pZCI6MSwiZXhwIjoxNTQ3ODA0MTk4LCJjaXR5X2lkIjoiMDAiLCJ0aW1lc3RhbXAiOjE1NDc4MDA1OTgwNDV9.I-MApLklxH4Zs3AGxWofwU3CLZpsFCFEY_knmrbqYVFuZbKnhHpZry3jzOXWa7aBoHbdLKz28ogIlIz9Y8OOCg’ ‘10.40.9.185:60001/cloud/ailk-cms-ms-alarmquery/size’ -d ‘{ “alarmtitle”:[“基站退服”,“小区退服”], “exResId”:[“2026413640”,“2031327029”], “alarmnetype”:[“BTS”,“NodeB”], “startEventTime”:1542816000, “endEventTime”:1542844800 }’ (绿色为请求参数中的键,红色为值,类似系统中处理页面的url?id=id) 只需要 HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(importProjectApiUrl2); req2.Headers.Add(“Authorization”, Authorization); --前面的Authorization为上面标记中绿色部分,后面的Authorization为上方标记中红色部分
|