gpt4 book ai didi

c# - 尝试在 c# 中上传到 Azure 时出现 400(未指定此请求必需的 HTTP header 。)

转载 作者:行者123 更新时间:2023-12-04 08:10:18 26 4
gpt4 key购买 nike

我正在尝试从 C# 上传一个简单的 emtpy .txt 到 Azure blob 存储(目前只是一个概念证明)。我使用 SAS token 来执行此操作(该 token 适用于迄今为止我尝试过的所有其他情况,例如列出容器或删除 blob)。但是,当尝试创建新的 blob 时,我收到错误 400(未指定此请求必需的 HTTP header 。)我想我以错误的方式配置了请求,但我无法找出正确的方式。有人可以帮忙吗? Put Rest API 调用可以找到 here .

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml.Linq;


namespace ConsoleProgram
{
public class Class1
{
private static string URL = "https://example.blob.core.windows.net/";
private static string sasToken = "sv=2019-12-12&ss=b&srt=sco&sp=rwdlacx&se=2021-02-02T15:52:32Z&st=2021-02-02T07:52:32Z&spr=https&sig=realSig";
private static string command = "";
private static string commandType = "Put";


static void PutBlob()
{
URL = "https://example.blob.core.windows.net/test/hello2.txt";
command = "?";
commandType = "Put";

}
static void ListContainers()
{
URL = "https://example.blob.core.windows.net/";
command = "?comp=list&";
commandType = "Get";
}
static void ListBlobs()
{
URL = "https://example.blob.core.windows.net/test";
command = "?restype=container&comp=list&";
commandType = "Get";
}
static void DeleteBlob()
{
URL = "https://example.blob.core.windows.net/test/hello2.txt";
command = "?";
commandType = "Delete";
}

static void Main(string[] args)
{
PutBlob();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(URL);

// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

HttpResponseMessage response;
// List data response.
if (commandType == "Get")
{
response = client.GetAsync(command + sasToken).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs.
}
else if (commandType == "Delete")
{
response = client.DeleteAsync(command + sasToken).Result;
}
else
{
// This is the Code that causes the problems

var s=new StringContent("hello2.txt");
response = client.PutAsync(command+sasToken,s).Result;
}

if (response.IsSuccessStatusCode)
{
Console.WriteLine("---------------Positive Responce----------------");
Console.WriteLine(response.ToString());
// Parse the response body.
var dataObjects = response.Content.ReadAsStringAsync().Result; //Make sure to add a reference to System.Net.Http.Formatting.dll
}
else
{
Console.WriteLine("---------------Negative Responce----------------");
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}

// Make any other calls using HttpClient here.

// Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
client.Dispose();
}


}
}

最佳答案

简单添加行:

 client.DefaultRequestHeaders.Add("x-ms-blob-type", "BlockBlob");

帮我修好了。

关于c# - 尝试在 c# 中上传到 Azure 时出现 400(未指定此请求必需的 HTTP header 。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66007653/

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