我对网络服务很陌生。现在我在使用 kraken.io API 来调整上传图像的大小时遇到问题。
请求响应时,它总是抛出异常。
任何帮助表示赞赏。非常感谢。
参考 kraken.io API 文档: https ://kraken.io/docs/upload-url
这是我到目前为止所做的
扳机:
byte[] data = new byte[fuImage.PostedFile.ContentLength];
fuImage.PostedFile.InputStream.Read(data, 0, fuImage.PostedFile.ContentLength);
objKraken krakenio = new objKraken();
krakenio.wait = true;
krakenio.resize = new objKResize() { width = Base_Controller.DealsWidth, height = Base_Controller.DealsHeight, strategy = "exact" };
Controller_Kraken.UploadFile(data, krakenio);
控制器:
public const string UploadAPIUrl = "https://api.kraken.io/v1/upload";
public static bool UploadFile(byte[] data, objKraken krakenInfo)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(UploadAPIUrl);
webRequest.Method = "POST";
webRequest.ContentType = "multipart/form-data";
string jsonString = JsonConvert.SerializeObject(krakenInfo);
webRequest.ContentLength = data.Length + jsonString.Length;
using (Stream postStream = webRequest.GetRequestStream())
{ // Send the data.
postStream.Write(data, 0, data.Length);
using (StreamWriter swRequest = new StreamWriter(postStream))
{
swRequest.Write(jsonString);
swRequest.Flush();
}
postStream.Close();
}
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
{
string responseText;
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
responseText = reader.ReadToEnd();
}
if (responseText == "true")
{ return true; }
else
{ return false; }
}
else
{ return false; }
}
}
catch
{ return false; }
}
目的:
public class objKraken
{
public objKAuth auth { get { return new objKAuth(); } }
public objKResize resize { get; set; }
public bool wait { get; set; }
}
public class objKAuth
{
public string api_key { get { return ConfigurationManager.AppSettings["ApiKey"]; } }
public string api_secret { get { return ConfigurationManager.AppSettings["SecretKey"]; } }
}
public class objKResize
{
public int width { get; set; }
public int height { get; set; }
public string strategy { get; set; }
}