如何在HtttpClient中将ProtoBuf作为字节发送?

时间:2017-11-10 11:18:16

标签: c# asp.net-web-api2 httpclient protocol-buffers

请找到我用于在HTTPCLIENT中发送字节的以下代码。

   public void SendProtoBufBytes(byte[] payload)
    {         

            #region WebAPI Call              

            using (HttpClient client = new HttpClient())
            {
                string baseUrl = ConfigurationManager.AppSettings["Environment"];
                client.BaseAddress = new Uri(baseUrl);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//For BINARY files - ("Content-type", "application/octet-stream");

                var byteArrayContent = new ByteArrayContent(payload);

                HttpResponseMessage response = client.PostAsync("api/processwebjobs/processjobrequest", byteArrayContent).Result;

                if (response.IsSuccessStatusCode)
                {
                    string processServerResponse = response.Content.ReadAsStringAsync().Result;
                }

           }

            #endregion
        }

此处,有效负载 ProtoBuf文件,该文件将转换为字节

WebApi方法:

[RoutePrefix("api/processwebjobs")]
public class ProcessWebJobsApiController : BaseApiController
{
    #region Public Api Methods

    [HttpPost]
    [Route("processjobrequest")]
    public IHttpActionResult ProcessJobRequest([FromBody] byte[] payload)
    {
    }
 }

感谢您的帮助!

0 个答案:

没有答案