使用C#将收据图像上传到Concur API

时间:2013-09-26 20:35:31

标签: c# api concur

我正在使用Concur的API并尝试将图片发布到费用条目,但我不断收到以下错误:

"Add expense entry image failed: Bad image sent. Possible reasons include image type in request header does not match actual image type, bad encoding or truncated content."

我发布到URI https://www.concursolutions.com/api/image/v1.0/expenseentry/ {id}并将内容类型指定为jpeg。

这是我的C#代码:

private static String PostReceipt(string token, string entryID, String pathToImageFile) {

    if (String.IsNullOrEmpty(token))
        throw new ArgumentNullException("token");

    if (String.IsNullOrEmpty(entryID))
        throw new ArgumentNullException("entryID");

    if (String.IsNullOrEmpty(pathToImageFile))
        throw new ArgumentNullException("pathToImageFile");

    if (!File.Exists(pathToImageFile))
        throw new FileNotFoundException("Could not find " + pathToImageFile + ".");

    byte[] bytes = File.ReadAllBytes(pathToImageFile);

    string URI = String.Format("https://www.concursolutions.com/api/image/v1.0/expenseentry/{0}", entryID);

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URI);
    webRequest.Method = "POST";
    webRequest.Headers.Add("Authorization", "OAuth " + token);
    webRequest.ContentType = "image/jpeg";

    using (Stream stream = webRequest.GetRequestStream())
    {
        stream.Write(bytes, 0, bytes.Length);
        stream.Flush();
    }

    XmlDocument xml = new XmlDocument();
    HttpWebResponse response = null;
    try
    {
        response = (HttpWebResponse)webRequest.GetResponse();
        xml.Load(response.GetResponseStream());
        return xml.SelectSingleNode("Image/Id").InnerText;
    }
    catch (WebException e)
    {
        response = (HttpWebResponse)e.Response;
        xml.Load(response.GetResponseStream());
        string errorMessage = xml.SelectSingleNode("Error/Message").InnerText;
        throw;
    }
}

我尝试了各种图像类型,并按照文档中的步骤操作,但找不到我做错的事。

1 个答案:

答案 0 :(得分:1)