要使用HttpWebRequest调用接收byte[]参数的Web服务操作,请按照以下步骤操作:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://example.com/webservice");
request.Method = "POST";
request.ContentType = "application/octet-stream";
byte[] data = new byte[] { 1, 2, 3, 4, 5 };
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string responseData = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
Console.WriteLine(responseData);
完整的代码示例如下:
using System;
using System.IO;
using System.Net;
class Program
{
static void Main(string[] args)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://example.com/webservice");
request.Method = "POST";
request.ContentType = "application/octet-stream";
byte[] data = new byte[] { 1, 2, 3, 4, 5 };
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string responseData = reader.ReadToEnd();
reader.Close();
responseStream.Close();
response.Close();
Console.WriteLine(responseData);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
这样,您就可以使用HttpWebRequest调用接收byte[]参数的Web服务操作了。
领取专属 10元无门槛券
手把手带您无忧上云