简体   繁体   中英

Translate cURL to c# or VB.net

I dont succeed to upload a file to cloud.seafile.com with my code.

The cURL string in the reference is:

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 works fine.

I tried many different approach with System.Net.WebClient or HttpResponse but nothing works.

As requested by comment, code incase the link gets deleted (you need 10K+ rep to view if so). The code is specific to multipart/form data with boundary (as per your -v specifies)

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;
    }
}

and then usage:

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);

What you are looking for is is covered in this question

You will need to add authentication to the request via the Authorization HTTP header:

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

I was tryng to upload files to Seafile cloud service. I already try the HttpUploadFile function. The raw request captured with fiddler is identical to cURL but for some reason it doesn't work.

I solved including curl.exe in my project running it in this way:

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM