1
  I am trying to upload file using httpwebrequest and rest api

我的代码是这样的,我没有收到任何错误,但我无法上传文件。 public static string RequestProfileUrl = "https://ws.onehub.com/workspaces/337426/folders/174352646/files/create?items[filename]=";

  if (file != null && file.ContentLength > 0)
        {
            byte[] bytearray = null;
            string name = "";
            long length = 0;
            string boundary = "----------------------------" +
            DateTime.Now.Ticks.ToString("x");
            name = file.FileName;
            Stream stream = file.InputStream;
            stream.Seek(0, SeekOrigin.Begin);
            bytearray = new byte[stream.Length];
            int count = 0;
            Stream memStream = new System.IO.MemoryStream();
            while (count < stream.Length)
            {
                bytearray[count++] = Convert.ToByte(stream.ReadByte());
            }
            //string baseAddress = "https://ws-api.onehub.com/workspaces/330201/files/";
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(RequestProfileUrl + name);
            request.Method = "PUT";
            request.ContentType = "multipart/form-data"; 
            request.ContentLength = bytearray.Length;
            request.GetRequestStream().Write(bytearray, 0, bytearray.Length);
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {

                int statusCode = (int)response.StatusCode;
                StreamReader reader = new StreamReader(response.GetResponseStream());
                string result = reader.ReadToEnd();
            }

我没有收到任何错误,但我的文件没有上传。我没有得到我的错误在哪里?任何问题都可以自由提问提前谢谢

4

0 回答 0