0

我正在使用以下代码测试 SkyDrive 的 API:

Dim webRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://apis.live.net/v5.0/me/skydrive/files?access_token=" & Me.ACCESS_TOKEN), HttpWebRequest)
    webRequest.Method = "POST"
    webRequest.ContentType = "multipart/form-data; boundary=A300x"
    webRequest.KeepAlive = True
    webRequest.Timeout = 80000
    Using streamWriter As New System.IO.StreamWriter(webRequest.GetRequestStream())
      streamWriter.Write("--A300x\r\n")
      streamWriter.Write("Content-Disposition: form-data; name=""file""; filename=""HelloWorld.txt""\r\n")
      streamWriter.Write("Content-Type: application/octet-stream\r\n")
      streamWriter.Write("\r\n")
      streamWriter.Write("vooo")
      streamWriter.Write("\r\n")
      streamWriter.Write("--A300x--\r\n")
      streamWriter.Close()
    End Using
    ' response
    Using webResponse As HttpWebResponse = DirectCast(webRequest, HttpWebRequest).GetResponse()
      If webResponse.StatusCode() = HttpStatusCode.OK Then
        Using streamReader As New System.IO.StreamReader(webResponse.GetResponseStream())
          Throw New Exception(streamReader.ReadToEnd())
          streamReader.Close()
        End Using
      End If
      webResponse.Close()
    End Using

收到错误请求 (400)。访问令牌和 URL 已验证且有效。任何线索?

4

2 回答 2

2

我使用以下方法将文件上传到 Skydrive。工作正常,至于我。

 public void uploadFile(string ParentFolderID, string FileName)
    {
        string uri = String.Format(SkyDriveHelper.BaseURI + ParentFolderID + "/files"+"?access_token=" + account.accessInfo.access_token);
        try
        {
            WebClient client = new WebClient();
            client.Headers.Add("Content-Type", "multipart/form-data; boundary=A300x");
            Stream requestStream = client.OpenWrite(uri);

            //writing start boundary
            requestStream.Write("--A300x\r\n".ToByteArray(), 0, "--A300x\r\n".Count());

            //writing body headers
            string header1 = String.Format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n", FileName);
            requestStream.Write(header1.ToByteArray(), 0, header1.Count());
            string header2 = "Content-Type: application/octet-stream\r\n";
            requestStream.Write(header2.ToByteArray(), 0, header2.Count());
            requestStream.Write("\r\n".ToByteArray(), 0, "\r\n".Count());

            //writing body - WRITE BYTES HERE 
            requestStream.Write("HelloWorld".ToByteArray(), 0, "HelloWorld".Count());

            //writing finish boundary
            requestStream.Write("\r\n\r\n".ToByteArray(), 0, "\r\n\r\n".Count());
            requestStream.Write("--A300x\r\n".ToByteArray(), 0, "--A300x\r\n".Count());
            requestStream.Close();

        }
        catch (Exception e)
        {
            MessageBox.Show("Download Error:" + e.Message);
        }

    }
于 2012-11-10T17:47:46.150 回答
0

如果您查看响应正文(或使用 Fiddler 来做同样的事情),您会发现一条 JSON 格式的错误消息,它准确地解释了问题所在。我的第一个猜测是该目标用户不存在文件夹skydrive 。

于 2012-08-17T00:03:59.023 回答