如何在使用post方法时传递标题和正文数据 [英] How to pass the header and body data while using post method

查看:98
本文介绍了如何在使用post方法时传递标题和正文数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个方法。一个方法用于传递数据通过正文我可以传递邮递员的身体数据,这是没有问题,但每当另一个方法使用传递数据通过邮递头的头时间如何使用post方法将
标题数据附加到body方法中。



身体数据代码



i have two methods.one method is used for passing the data through the body at the time i can possible to pass the body data in postman that is not problem but whenever another method used passing the data through the header in postman at the time how
header data attached to the body method while using post method.

Body data code

[Httppost]
[Route("api/Account/Login")]
public async task<getbasecustomerresponse> Login([From body]LoginRequest request)
{

   
   

 return response;
}





标题数据代码





Header data code

private HttpWebRequest CreateRequestObject(string ServiceURL)
{
   HttpWebRequest request=null;
   try
   {
      var _authkey=configurationManger.APPSetting["userkey"];
      request=(HttpWebRequest)WebRequest.Create(ServiceURL)
      request.Method="Post";
      request.ContentType="Application/Json";
      request.Headers.Add("AuthKey",_authkey);
   }
   catch(Exception ex)
  {
  }
 return request

}













请帮帮我,



谢谢你。



我尝试过:



i有两个方法。一个方法用于传递通过身体的数据,我可以在邮递员中传递身体数据,这是没有问题,但每当另一种方法使用传递数据通过邮递员的头当时如何头和身体方法传递到网址,即如何使用服务时的头和主体方法。







please help me,

Thank u.

What I have tried:

i have two methods.one method is used for passing the data through the body at the time i can possible to pass the body data in postman that is not problem but whenever another method used passing the data through the header in postman at the time how header and body method communicate to the url i.e how to use header and body method at time for service.

推荐答案

你可以通过写入字节来发送数据(在本例中查看内容类型的json)(将json字符串转换为字节)到请求流。



还初始化标题中的字节长度



You can send data (json in this case looking at the content type) by writing the bytes (convert json string to bytes) to the request stream.

Also initialize the length of bytes in header

request.ContentLength = bytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Close();


这篇关于如何在使用post方法时传递标题和正文数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆