在C#中使用HttpWebRequest将带有元数据的文件上传到API时出错

时间:2019-05-23 11:25:10

标签: c# asp.net-mvc api httpwebrequest

我正在使用API​​使用C#中的HttpWebRequest类上传带有元数据的文件,但是在调试代码时,它返回的错误是400错误。

请帮助我找出问题所在。我已在下面附加了API参考图片。

enter image description here 下面,我附上了必须传递的元数据

{
"summary":"test",
"owners":[
"TEST_user"
],
"managers":[
"TESTECM.TESTECM"
],
"receivedDate":"2016-07-25T16:54:34+1000",
"status":"20",
"folders":[
"b26e2b79-1693-4138-b42e-ac49b46104eb"
],
"docType":"INT",
"contentType":"1",
"extRefNo":"123456",
"htmlContent":"Sample Content"
}

贝洛我附上了我尝试过的C#代码

public class FileUploadMetaDatas
{
    public string summary = "test";
    // public string owners = "TESTECM";
    public string[] owners = new string[] { "TESTECM_user" };
    //public string managers = "TESTECM.TESTECM";
    public string[] managers = new string[] { "TESTECM.TESTECM" };
    public string receivedDate = DateTime.Now.ToString();
    public string status = "20";
    //public string folders = "b26e2b79-1693-4138-b42e-ac49b46104eb";
    public string[] folders = new string[] { "b26e2b79-1693-4138-b42e-ac49b46104eb" };
    public string docType = "INT";
    public string contentType = "1";
    public string extRefNo = "123456";
    public string htmlContent = "Sample Content";
}
class Program
{
    static void Main(string[] args)
    {
        try
        {
            var AccessTokenValue = "1ybht6c-a0ef-47e6-9d57-0655tygy73952";
            FileUploadMetaDatas FUMD = new FileUploadMetaDatas();
            var Imagefile = "https://www.logaster.com/blog/wp-content/uploads/2013/06/jpg.png";

            string jsonMetaModel = Newtonsoft.Json.JsonConvert.SerializeObject(FUMD);
            string postFileData = "meta=" + jsonMetaModel + "&file=" + Imagefile;
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] filebytess = encoding.GetBytes(postFileData);
            string Uploaduri = "https://testwebserverdev.test.com:8493/testecm/rest/docs";

            HttpWebRequest requestUpload = (HttpWebRequest)WebRequest.Create(Uploaduri);
            requestUpload.Method = "POST";
            requestUpload.PreAuthenticate = true;
            requestUpload.Headers.Add("Authorization", "Bearer " + AccessTokenValue);
            requestUpload.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            requestUpload.ContentType = "multipart/form-data";
            //requestUpload.Accept = "application/json";
            requestUpload.ContentLength = filebytess.Length;

            Stream newStream = requestUpload.GetRequestStream();
            newStream.Write(filebytess, 0, filebytess.Length);

            HttpWebResponse respofnse = (HttpWebResponse)requestUpload.GetResponse();

            // get the response content
            StreamReader reader = new StreamReader(respofnse.GetResponseStream(), Encoding.UTF8);
            var respofnskuie = reader.ReadToEnd();
            //Console.WriteLine(respofnskuie);
            dynamic obj = JsonConvert.DeserializeObject(respofnskuie);
            Console.WriteLine(obj.access_token);
            reader.Close();
            reader.Dispose();

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex+"NOT WORKING");
        }                    
    }                                                 
}

0 个答案:

没有答案