C# 使用httpwebrequest和restapi上传文件
我的代码是这样的,我没有得到任何错误,但我不能上传文件。 公共静态字符串RequestProfileUrl=https://ws.onehub.com/workspaces/337426/folders/174352646/files/create?items[文件名]= 我没有收到任何错误,但我的文件没有上传。我不明白我的错误在哪里? 有什么问题可以问吗C# 使用httpwebrequest和restapi上传文件,c#,asp.net,rest,file-upload,httpwebrequest,C#,Asp.net,Rest,File Upload,Httpwebrequest,我的代码是这样的,我没有得到任何错误,但我不能上传文件。 公共静态字符串RequestProfileUrl=https://ws.onehub.com/workspaces/337426/folders/174352646/files/create?items[文件名]= 我没有收到任何错误,但我的文件没有上传。我不明白我的错误在哪里? 有什么问题可以问吗 提前感谢WebResponse结果包含什么?它包含登录页面的html设计页面。那么,您必须首先进行身份验证,并在每个请求中传递cookies
提前感谢WebResponse结果包含什么?它包含登录页面的html设计页面。那么,您必须首先进行身份验证,并在每个请求中传递cookies。启用cookie的WebClient会很好地工作。我的代码是否可以上载文件,或者不可以。如何在每个请求中传递cookie?
I am trying to upload file using httpwebrequest and rest api
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();
}