I'm pretty new to web services. Right now I have problem with using the kraken.io API to resize an uploaded image.
When requesting the response, it always throws an exception.
Any help is appreciated. Thank you very much.
Reference to kraken.io API Docs: https://kraken.io/docs/upload-url
This is what I have done so far
Trigger:
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);
Controller:
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; }
}
Object:
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; }
}