问下各位,我 c#调用httppost调用webapi,也用postman做了测试是可以的,也写了代码了,为何我程序端运行,还是出错?
我在c# winform程序里调用第三方提供的api,也通过postman测试是正常的,现在通过c#写了一段函数,访问,一直有问题。
1.postman的测试ui header部分

2.postman的测试ui body部分

3.我的访问的函数代码如下:
public static string PostFunc(string apiUrl, string token, string param)
{
string strJson = string.Empty;
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
request = WebRequest.Create(apiUrl) as HttpWebRequest;
request.AllowWriteStreamBuffering = true;//
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
byte[] b = Encoding.UTF8.GetBytes(param);
//Header中添加token验证(服务器要求)
request.Headers.Add("eg-token", token);//添加token到header
request.Headers.Add("Content-Type", "application/json");
//request.ContentType = "application/json";//已有token使用json类型
request.ContentLength = b.Length;
request.Method = "POST";
if (b.Length > 0)
{
using (Stream stream = request.GetRequestStream())
{
stream.Write(b, 0, b.Length);
stream.Close();
}
}
//获取服务器返回
using (response = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
strJson = reader.ReadToEnd();
reader.Close();
}
}
return strJson;
}
catch (Exception exception)
{
return exception.Message;
}
finally
{
if (response != null)
response.Close();
}
}
对照postman方式,请问如何怎么修改?我红色部分代码
1.postman的测试ui header部分

2.postman的测试ui body部分

3.我的访问的函数代码如下:
public static string PostFunc(string apiUrl, string token, string param)
{
string strJson = string.Empty;
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
request = WebRequest.Create(apiUrl) as HttpWebRequest;
request.AllowWriteStreamBuffering = true;//
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
byte[] b = Encoding.UTF8.GetBytes(param);
//Header中添加token验证(服务器要求)
request.Headers.Add("eg-token", token);//添加token到header
request.Headers.Add("Content-Type", "application/json");
//request.ContentType = "application/json";//已有token使用json类型
request.ContentLength = b.Length;
request.Method = "POST";
if (b.Length > 0)
{
using (Stream stream = request.GetRequestStream())
{
stream.Write(b, 0, b.Length);
stream.Close();
}
}
//获取服务器返回
using (response = request.GetResponse() as HttpWebResponse)
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
strJson = reader.ReadToEnd();
reader.Close();
}
}
return strJson;
}
catch (Exception exception)
{
return exception.Message;
}
finally
{
if (response != null)
response.Close();
}
}
对照postman方式,请问如何怎么修改?我红色部分代码
...全文
相关推荐
金蝶云 自定义WebAPI接口 c#程序示例.rar
金蝶云 自定义WebAPI接口 c#程序示例.rar
基于.NET的WebAPI HTTPPost服务构建与实战
ASP.NET Web API是微软为构建HTTP服务而设计的轻量级框架,专用于暴露基于RESTful风格的API接口。其核心依托HTTP协议本身的语义(如GET、POST、PUT、DELETE),实现资源的标准化访问与跨平台交互,广泛应用于Web、移动端及IoT系统集成中。框架采用消息处理管道模型,通过链式处理请求与响应,并内置内容协商机制(Content Negotiation),自动根据客户端Accept头返回JSON或XML格式数据。
net webApi 实例
net webApi 实例,通过后台get和post方式提交数据
.Net Core WebApi的简单创建以及使用方法
主要给大家介绍了关于.Net Core WebApi的简单创建以及使用方法的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.Net Core WebApi具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
WebAPI:完整的Web API
WebAPI 完整的Web API


Web Services
