Asp.net 如果设置ContentLength>;0或SendChunked==true。通过在[Begin]GetResponse之前调用[Begin]GetRequestStream来实现这一点
我试图在oracle云基础设施iaas上上载文件,但出现错误。我不确定我在正文中附加的文件是否在 格式是否正确。ApI签名是正确的,我只对 我写的代码是否符合要求。下面提到了代码片段。 代码片段:Asp.net 如果设置ContentLength>;0或SendChunked==true。通过在[Begin]GetResponse之前调用[Begin]GetRequestStream来实现这一点,asp.net,oracle-cloud-infrastructure,Asp.net,Oracle Cloud Infrastructure,我试图在oracle云基础设施iaas上上载文件,但出现错误。我不确定我在正文中附加的文件是否在 格式是否正确。ApI签名是正确的,我只对 我写的代码是否符合要求。下面提到了代码片段。 代码片段: FileInfo f = new FileInfo(FileUpload1.FileName); byte[] filebyte =FileUpload1.FileBytes; var postdata = Encoding.U
FileInfo f = new FileInfo(FileUpload1.FileName);
byte[] filebyte =FileUpload1.FileBytes;
var postdata = Encoding.UTF8.GetBytes(filebyte.ToString());
Console.Write(postdata.Length);
var tenancyId = ConfigurationManager.AppSettings["BMCTenancyId"];
var userId = ConfigurationManager.AppSettings["BMCUserId"];
var fingerprint = ConfigurationManager.AppSettings["BMCFingerprint"];
var privateKeyPath = ConfigurationManager.AppSettings["BMCPrivateKeyPath"];
var privateKeyPassphrase = ConfigurationManager.AppSettings["BMCPrivateKeyPassphrase"];
var signer = new RequestSigner(tenancyId, userId, fingerprint, privateKeyPath, privateKeyPassphrase);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var uri = new Uri($"https://objectstorage.us-phoenix-1.oraclecloud.com/");
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.Accept = "application/json";
request.SendChunked = true;
request.ContentType = "text/plain";
request.ContentLength =postdata.Length;
try
{
using (var stream = request.GetRequestStream())
{
stream.Write(postdata, 0, postdata.Length);
stream.Close();
}
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
request.Headers["x-content-sha256"] = Convert.ToBase64String(SHA256.Create().ComputeHash(postdata));
signer.SignRequest(request);
Console.WriteLine($"Authorization header: {request.Headers["authorization"]}");
ExecuteRequest(request);
Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", request.ContentLength);
}
private static void ExecuteRequest(HttpWebRequest request)
{
try
{
var webResponse = (HttpWebResponse)request.GetResponse();
var response = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
Console.WriteLine($"Response: {response}");
}
catch (WebException e)
{
Console.WriteLine($"Exception occurred: {e.Message}");
Console.WriteLine($"Response: {new StreamReader(e.Response.GetResponseStream()).ReadToEnd()}");
}
}
首先,您需要将URL更新为以下格式:
var uri = new Uri($"https://objectstorage.us-phoenix-1.oraclecloud.com/n/{namespaceName}/b/{bucketName}/o/{objectName}");
文件:
另外,您可以编辑问题以包含您收到的完整错误,这将有助于调试