【问题标题】:Translate cURL to c# or VB.net将 cURL 翻译成 c# 或 VB.net
【发布时间】:2014-06-01 20:00:43
【问题描述】:

我没有成功使用我的代码将文件上传到 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

使用 cURL 可以正常工作。

我用 System.Net.WebClient 或 HttpResponse 尝试了许多不同的方法,但没有任何效果。

【问题讨论】:

  • 向我们展示您的代码。另外,请使用HttpClient
  • 您是否已将所有必需的标头添加到您的 .NET 请求中?使用带有 curl 的“-v”标志来显示 curl 随您的请求发出的所有标志。
  • 我不能使用 HttpClient,因为我必须使用 Framework 4 Client Profile。
  • 使用 cURL -v 我明白了 > POST /seafhttp/upload-api/26af9fe1 HTTP/1.1 > 用户代理:curl/7.37.0 > 主机:cloud.seafile.com > 接受:/ > 授权:令牌 6a3ff5c8c2127baf8e1d94d928acc477f72a9519 > 内容长度:6099 > 预期:100-继续 > 内容类型:multipart/form-data;边界=------------50967bf4b0 e5362d >
  • 请在问题中添加对问题的补充,而不是在 cmets 中。

标签: c# vb.net curl webclient httpresponse


【解决方案1】:

根据评论的要求,代码以防链接被删除(如果是这样,您需要 10K+ 代表才能查看)。 该代码特定于具有边界的多部分/表单数据(根据您的 -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);

【讨论】:

    【解决方案2】:

    this question 涵盖了您要查找的内容

    您需要通过Authorization HTTP 标头为请求添加身份验证:

    req.Headers.Add("Authorization", auth);
    

    【讨论】:

    • 我确实包含了授权标头。
    【解决方案3】:

    我正在尝试将文件上传到 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-10
      • 1970-01-01
      相关资源
      最近更新 更多