C#模拟传递参数为json格式的POST提交
今天在开发过程中遇到一个问题,在用PostMan测试调用另一个公司的接口时一直不成功,然后经查看,发现该接口需要传递json格式的值。
C#模拟提交代码如下:
public ActionResult GetCarLocation(string vin)
{
RequestModel model = new RequestModel() { pageNum=0, pageSize=0, vinNum=vin };
string param= JsonConvert.SerializeObject(model);
string returnstr= PostMoths("http://188.88.88.88:8081/third/carinfo/list", param);
return Content(returnstr) ;
}
public static string PostMoths(string url, string param)
{
string strURL = url;
System.Net.HttpWebRequest request;
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = param;
byte[] payload;
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, 0, payload.Length);
writer.Close();
System.Net.HttpWebResponse response;
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s;
s = response.GetResponseStream();
string StrDate = "";
string strValue = "";
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
return strValue;
}
问题解决。
版权声明:
作者:兴兴
文章:C#模拟传递参数为json格式的POST提交
链接:https://www.networkcabin.com/original/1338
文章版权归本站所有,未经授权请勿转载。
作者:兴兴
文章:C#模拟传递参数为json格式的POST提交
链接:https://www.networkcabin.com/original/1338
文章版权归本站所有,未经授权请勿转载。
THE END