远程服务器返回错误:(400)Bad Request.3

时间:2014-12-24 08:09:28

标签: c# truevault

在使用truevault嵌入web进行数据存储时,在上传文件期间以及通过truevault获取文档ID时我得到了远程服务器返回错误:(400)运行以下代码时出现错误请求错误。而在其他页面上相同的代码工作正常

string documentId ="&#34 ;;

        string jsonDocument = JsonConvert.SerializeObject(NewsInfo);

        string encodedJson = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(jsonDocument));
        string formVars = "document=" + encodedJson;
        string url = "https://api.truevault.com/v1/vaults/" + vaultId + "/documents";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = "application/x-www-form-urlencoded";
        request.Accept = "*/*";
        request.Method = WebRequestMethods.Http.Post;
        request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(apiKey + ":")));

        byte[] byteArray = Encoding.ASCII.GetBytes(formVars);
        request.ContentLength = byteArray.Length;
        Stream stream = request.GetRequestStream(); //open connection
        stream.Write(byteArray, 0, byteArray.Length); // Send the data.
        stream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        string html = streamReader.ReadToEnd();

        if (response.StatusCode == HttpStatusCode.OK)
        {
            JToken token = JObject.Parse(html).SelectToken("document_id");
            documentId = (string)token;
        }

        response.Close();
        streamReader.Close();

        return documentId;

1 个答案:

答案 0 :(得分:0)

如果您要上传文件,则可能需要使用multipart/form-data。 并非总是如此,但通常,这是正确的做法。

同时检查truevault允许的最大数据大小。

如果有帮助,请告诉我。