gpt4 book ai didi

c# - 将字节数组从一个站点发送到另一个站点(并返回)

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:09:44 26 4
gpt4 key购买 nike

我控制这两个站点,所以任何方法都可以。

必须有比以下更简单的方法:

byte[] result;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://blahblah.com/blah.ashx");
byte[] inputToSend = new byte[] { 1, 2, 3 };
request.Method = "POST";
request.ContentType = "image/jpeg";
request.Timeout = 30 * 1000;
request.ContentLength = inputToSend.Length;
using (Stream stream = request.GetRequestStream())
stream.Write(inputToSend, 0, inputToSend.Length);
request.Headers.Add("blah", "more blah");//This is for authentication.
WebResponse r = request.GetResponse();
using (MemoryStream ms = new MemoryStream())
{
r.GetResponseStream().CopyTo(ms);
result = ms.ToArray();
}

不是吗?

(代码是请求方,响应方更简单)

最佳答案

您可能会使用 WebClient 来使代码更小。具体来说,UploadData方法:

using (var wc = new WebClient()) {
wc.UploadData(yourUrl, inputToSend);
}

..并下载:

using (var wc = new WebClient()) {
var receivedData = wc.DownloadData(yourUri);
}

您可以通过 WebClient Headers 属性添加您需要的任何 header :

wc.Headers.Add("blah", "blah"); // your auth stuff here.

关于c# - 将字节数组从一个站点发送到另一个站点(并返回),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20205223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com