请找到我用于在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)
{
}
}
感谢您的帮助!