Quickblox 上传文件 rest api 在 c# 中调用给 400
Quickblox Upload file rest api call in c# giving 400
我正在使用 api 上传头像...参考 link :http://quickblox.com/developers/Content#API_Content_Upload_File
var uri = new Uri(getparam);
var query = HttpUtility.ParseQueryString(uri.Query);
var ContentType = query.Get("Content-Type");
var Expires = query.Get("Expires");
var acl = query.Get("acl");
var key = query.Get("key");
var policy = query.Get("policy");
var successactionstatus = query.Get("success_action_status");
var xamzalgorithm = query.Get("x-amz-algorithm");
var xamzcredential = query.Get("x-amz-credential");
var xamzdate = query.Get("x-amz-date");
var xamzsignature = query.Get("x-amz-signature");
string profileimagepath = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/ProfileImage/"));
var fullpath=profileimagepath + newFileName;
var fileName = System.IO.Path.GetFileName(Convert.ToString(fullpath));
using (var wb = new WebClient())
{
var data = new NameValueCollection();
data["Content-Type"] = ContentType;
data["Expires"] = Expires;
data["acl"] = acl;
data["key"] = key;
data["policy"] = policy;
data["success_action_status"] = successactionstatus;`enter code here`
data["x-amz-algorithm"] = xamzalgorithm;
data["x-amz-credential"] = xamzcredential;
data["x-amz-date"] = xamzdate;
data["x-amz-signature"] = xamzsignature;
//data["file"] = ImageToBase64(fullpath);
data["file"] = @filename;
wb.UploadFile("https://qbprod.s3.amazonaws.com/", "POST", fullpath);
var qbUploadFileResponse = wb.UploadValues("https://qbprod.s3.amazonaws.com/", data);
}
它在 wb.UploadValues() 中给了我 400 个错误请求。它在休息客户端(邮递员)中工作完美。
请帮忙
您应该使用 http 客户端并作为 MultipartFormDataContent 发送,而不是使用 Web 客户端。它对我有用!!!!
HttpContent bytesContent = new ByteArrayContent(ImageToBase64(fullpath));
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(new StringContent(ContentType), "Content-Type");
formData.Add(new StringContent(Expires), "Expires");
formData.Add(new StringContent(acl), "acl");
formData.Add(new StringContent(key), "key");
formData.Add(new StringContent(policy), "policy");
formData.Add(new StringContent(successactionstatus), "success_action_status");
formData.Add(new StringContent(xamzalgorithm), "x-amz-algorithm");
formData.Add(new StringContent(xamzcredential), "x-amz-credential");
formData.Add(new StringContent(xamzdate), "x-amz-date");
formData.Add(new StringContent(xamzsignature), "x-amz-signature");
formData.Add(bytesContent, "file", fileName);
var responseTemp = client.PostAsync("https://qbprod.s3.amazonaws.com/", formData).Result;
if (!responseTemp.IsSuccessStatusCode)
{
resultResponse.ErrorCode = "0";
resultResponse.Message = "Profile Save Succesfully. But Quick Bolx Blob A/c Not Updated";
resultResponse.Data = response.Response;
return Ok(resultResponse);
}
var respon = responseTemp.Content.ReadAsStreamAsync().Result;
if (respon != null)
{
var finalResponse = DeclaringFileUpload(gettokenbyuser, Quickbolxblob_id);
////// convert image in Base64 byte.
public byte[] ImageToBase64(string path)
{
using (Image image = Image.FromFile(path))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return imageBytes;
}
}
}
我正在使用 api 上传头像...参考 link :http://quickblox.com/developers/Content#API_Content_Upload_File
var uri = new Uri(getparam);
var query = HttpUtility.ParseQueryString(uri.Query);
var ContentType = query.Get("Content-Type");
var Expires = query.Get("Expires");
var acl = query.Get("acl");
var key = query.Get("key");
var policy = query.Get("policy");
var successactionstatus = query.Get("success_action_status");
var xamzalgorithm = query.Get("x-amz-algorithm");
var xamzcredential = query.Get("x-amz-credential");
var xamzdate = query.Get("x-amz-date");
var xamzsignature = query.Get("x-amz-signature");
string profileimagepath = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/ProfileImage/"));
var fullpath=profileimagepath + newFileName;
var fileName = System.IO.Path.GetFileName(Convert.ToString(fullpath));
using (var wb = new WebClient())
{
var data = new NameValueCollection();
data["Content-Type"] = ContentType;
data["Expires"] = Expires;
data["acl"] = acl;
data["key"] = key;
data["policy"] = policy;
data["success_action_status"] = successactionstatus;`enter code here`
data["x-amz-algorithm"] = xamzalgorithm;
data["x-amz-credential"] = xamzcredential;
data["x-amz-date"] = xamzdate;
data["x-amz-signature"] = xamzsignature;
//data["file"] = ImageToBase64(fullpath);
data["file"] = @filename;
wb.UploadFile("https://qbprod.s3.amazonaws.com/", "POST", fullpath);
var qbUploadFileResponse = wb.UploadValues("https://qbprod.s3.amazonaws.com/", data);
}
它在 wb.UploadValues() 中给了我 400 个错误请求。它在休息客户端(邮递员)中工作完美。
请帮忙
您应该使用 http 客户端并作为 MultipartFormDataContent 发送,而不是使用 Web 客户端。它对我有用!!!!
HttpContent bytesContent = new ByteArrayContent(ImageToBase64(fullpath));
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
formData.Add(new StringContent(ContentType), "Content-Type");
formData.Add(new StringContent(Expires), "Expires");
formData.Add(new StringContent(acl), "acl");
formData.Add(new StringContent(key), "key");
formData.Add(new StringContent(policy), "policy");
formData.Add(new StringContent(successactionstatus), "success_action_status");
formData.Add(new StringContent(xamzalgorithm), "x-amz-algorithm");
formData.Add(new StringContent(xamzcredential), "x-amz-credential");
formData.Add(new StringContent(xamzdate), "x-amz-date");
formData.Add(new StringContent(xamzsignature), "x-amz-signature");
formData.Add(bytesContent, "file", fileName);
var responseTemp = client.PostAsync("https://qbprod.s3.amazonaws.com/", formData).Result;
if (!responseTemp.IsSuccessStatusCode)
{
resultResponse.ErrorCode = "0";
resultResponse.Message = "Profile Save Succesfully. But Quick Bolx Blob A/c Not Updated";
resultResponse.Data = response.Response;
return Ok(resultResponse);
}
var respon = responseTemp.Content.ReadAsStreamAsync().Result;
if (respon != null)
{
var finalResponse = DeclaringFileUpload(gettokenbyuser, Quickbolxblob_id);
////// convert image in Base64 byte.
public byte[] ImageToBase64(string path)
{
using (Image image = Image.FromFile(path))
{
using (MemoryStream m = new MemoryStream())
{
image.Save(m, image.RawFormat);
byte[] imageBytes = m.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return imageBytes;
}
}
}