翻译cURL到c#或VB.net
本文关键字:VB net cURL 翻译 | 更新日期: 2023-09-27 18:06:21
我的代码没有成功上传文件到cloud.seafile.com。
引用中的cURL字符串是:
curl -H "Authorization: Token 6a3ff5c8c2127baf8e1d94d928acc477f72a9519" -F file=@"C:'vcredist.bmp" -F filename=vcredist.bmp -F parent_dir=/ https://cloud.seafile.com/seafhttp/upload-api/26af9fe1
With cURL工作得很好
我尝试了许多不同的方法与System.Net.WebClient或HttpResponse,但没有工作。
根据注释的要求,删除链接中的代码(如果是这样,您需要10K+ rep才能查看)。该代码特定于具有边界的多部分/表单数据(根据-v指定)
public static void HttpUploadFile(string url, string file, string paramName, string contentType, NameValueCollection nvc) {
log.Debug(string.Format("Uploading {0} to {1}", file, url));
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("'r'n--" + boundary + "'r'n");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream rs = wr.GetRequestStream();
string formdataTemplate = "Content-Disposition: form-data; name='"{0}'"'r'n'r'n{1}";
foreach (string key in nvc.Keys)
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, key, nvc[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
}
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name='"{0}'"; filename='"{1}'"'r'nContent-Type: {2}'r'n'r'n";
string header = string.Format(headerTemplate, paramName, file, contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) {
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("'r'n--" + boundary + "--'r'n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try {
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
log.Debug(string.Format("File uploaded, server response is: {0}", reader2.ReadToEnd()));
} catch(Exception ex) {
log.Error("Error uploading file", ex);
if(wresp != null) {
wresp.Close();
wresp = null;
}
} finally {
wr = null;
}
}
和用法:
NameValueCollection nvc = new NameValueCollection();
nvc.Add("id", "TTR");
nvc.Add("btn-submit-photo", "Upload");
HttpUploadFile("http://your.server.com/upload",
@"C:'test'test.jpg", "file", "image/jpeg", nvc);
你要找的是在这个问题中包含的
您需要通过Authorization
HTTP头向请求添加身份验证:
req.Headers.Add("Authorization", auth);
我正在尝试上传文件到Seafile云服务。我已经尝试了HttpUploadFile函数。用fiddler捕获的原始请求与cURL相同,但由于某种原因它不起作用。
我解决了在我的项目中包含curl.exe,以这种方式运行它:
Dim pInfo As New ProcessStartInfo(cURLPath, "-H ""Authorization: Token " & Token & """ -F file=@""" & FilePathToUpload & """ -F filename=" & FileNameToUpload & " -F parent_dir=/ " & UploadURL)
pInfo.WindowStyle = ProcessWindowStyle.Hidden
pInfo.UseShellExecute = False
Process.Start(pInfo)