【问题标题】:HttpWebRequest to upload docx file to curl python from c#HttpWebRequest从c#上传docx文件到curl python
【发布时间】:2018-05-25 07:53:50
【问题描述】:

这是我用来从 ubuntu 调用的:

curl -X POST http://192.168.XX.XX:XXXX -F 'file[]=@file1.docx' -i

我想从 c# 中执行这个。

注意:我尝试了其他 stackoverflow 答案/解决方案,但我总是得到: internal server error : 505

【问题讨论】:

    标签: c# python httpwebrequest docx


    【解决方案1】:

    我可以使用以下代码成功调用它。

    public static string HttpUploadFile(string file)
    {
        string paramName = "file[]";
        string[] result = null;
        string url = "http://192.168.XX.XX:XXXX";
        string contenttype_xlsx = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        string contenttype_pptx = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
        string contenttype_docx = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";         
        string contentType = "";
    
        switch (Path.GetExtension(file))
        {
            case ".docx":
                contentType = contenttype_docx;
                break;
    
            case ".pptx":
                contentType = contenttype_pptx;
                break;
    
            default:
                contentType = contenttype_xlsx;
                break;
        }            
    
        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();
    
        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, Path.GetFileName(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, Encoding.UTF8);
            string s = reader2.ReadToEnd();
    
            JObject obj = JObject.Parse(s);        
        }
        catch (Exception ex)
        {
            if (wresp != null)
            {
                wresp.Close();
                wresp = null;
            }
            throw new ApplicationException(ex.Message + ex.StackTrace);
        }
        finally
        {
            wr = null;
        }
    
        return obj.ToString();
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-13
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2021-12-27
      • 2015-09-20
      • 2012-03-02
      • 1970-01-01
      • 2021-02-01
      相关资源
      最近更新 更多