private static string url = "https://localhost:44317/api/";
public static string PostRequestAPI(string apiName, Dictionary<string, object> parameters)
{
//实例化
WebClient client = new WebClient();
//地址
string requestUrl = url + apiName;
//数据较大的参数
string datastr = "";
foreach (var item in parameters)
{
datastr += item.Key + "=" + item.Value + "&";
}
datastr = datastr.Substring(0, datastr.Length - 1);
//参数转流
byte[] bytearray = Encoding.UTF8.GetBytes(datastr);
string myParameters = "key=" + "123" + "&system_id=" + "123" + "";
byte[] aaddd = Encoding.UTF8.GetBytes(myParameters);
//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//长度
client.Headers.Add("ContentLength", bytearray.Length.ToString());
//上传,post方式,并接收返回数据(这是同步,需要等待接收返回值)
byte[] responseData = client.UploadData(requestUrl, aaddd);
//释放
client.Dispose();
//处理返回数据(一般用json)
string srcString = Encoding.UTF8.GetString(responseData);
return srcString;
}
public class use
{
public string name { get; set; }
public string sex { get; set; }
}
/// <summary>
/// Get方式请求接口
/// </summary>
/// <param name="apiNamespace"></param>
/// <param name="apiName"></param>
/// <param name="parameters"></param>
public static string GetRequestAPI(string apiNamespace, string apiName, Dictionary<string, object> parameters)
{
//地址
//string url = "https://gw.open.1688.com/openapi/param2/1/" + apiNamespace + "/" + apiName + "/";// + StaticInfo.AppKey + "?";
string url = "http://192.168.2.237/api/" + apiNamespace + "/" + apiName + "?";
foreach (var item in parameters)
{
url += item.Key + "=" + item.Value + "&";
}
url = url.Substring(0, url.Length - 1);
//实例化
WebClient client = new WebClient();
//上传并接收数据
Byte[] responseData = client.DownloadData(url);
//接收返回的json的流数据,并转码
string result = Encoding.UTF8.GetString(responseData);
return result;
}
/// <summary>
/// Get方式请求接口
/// </summary>
/// <param name="url">请求地址</param>
/// <returns></returns>
public static string GetRequestAPI(string url)
{
//实例化
WebClient client = new WebClient();
//上传并接收数据
Byte[] responseData = client.DownloadData(url);
//接收返回的json的流数据,并转码
string result = Encoding.UTF8.GetString(responseData);
return result;
}
get、post方法请求接口,测试方法
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 发布博客只是记录下自己对自动化测试框架的一个经历过程,框架整体是有Python+unittest框架完成的,...
- https://api.douban.com/v2/book/search?q=自动化测试&start=0&cou...
- 现在的模拟发送请求插件很多比如老外的postman等,但亲测咱们国内的 ApiPost 更好用一些,因为它不仅可以...
- post请求接口测试-加强通用版 知道了get请求接口如何写了,post就非常简单了,基本上就是照猫画虎即可,主要...
- 马上五一了,首先祝大家五一快乐。 此篇主要介绍SoapUI工具做常用的两种请求接口测试,分别是get请求和post...