0

重新连接到 webdav.yandex.ru。应用 PUT。将文件传输到服务器。写入错误 409。求助,我做错了

示例 API http://api.yandex.ru/disk/doc/dg/reference/put.xml

public static bool UploadFile(string localFile, string uploadUrl)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);

        try
        {
            req.Method = "PUT";
            req.Host = "webdav.yandex.ru";
            req.Accept = "*/*";
            req.Headers.Add("Authorization: OAuth 0c4181a7c2cf4521964a72ff57a34a07");
            req.ServicePoint.Expect100Continue = true;
            req.ContentType = "application/binary";
            req.ContentLength = 1024;
            req.KeepAlive = false;

            Stream reqStream = req.GetRequestStream();
            FileStream rdr = new FileStream(localFile, FileMode.Open, FileAccess.Read);
            byte[] inData = new byte[1024];
            int bytesRead = rdr.Read(inData, 0, inData.Length);

            while (bytesRead > 0)
            {
                reqStream.Write(inData, 0, bytesRead);
                bytesRead = rdr.Read(inData, 0, inData.Length);
            }
            reqStream.Close();
            rdr.Close();

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

            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
            {
                Console.WriteLine("Couldn't upload file");
                return false;
            }
        }
        catch (Exception ex)
        {

            Console.WriteLine(ex.ToString());
            return false;
        }
        return true;
    }
4

0 回答 0