【问题标题】:How send a PDF by POST HttpWebRequest in C#如何在 C# 中通过 POST HttpWebRequest 发送 PDF
【发布时间】:2013-06-06 07:15:19
【问题描述】:

(我是法国人,对不起我的英语)
我不知道/不了解如何通过 web 服务(REST 协议)的 post 请求发送简单的 PDF 文件。
我尝试了一些例子,但它没有字。当我使用 a 时,它可以工作,但我只想在后面的代码中执行此操作!
我的问题是标题:如何发送此 PDF 文件?
网址:https://test.website.fr/Website/api/transactions/" + sVal + "/contrat
PDF 文件:questions.pdf

谢谢。 虹膜触摸

【问题讨论】:

    标签: c# rest pdf post httpwebrequest


    【解决方案1】:

    我找到了解决方案:

    //Identificate separator
    string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
    //Encoding
    byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
    
    //Creation and specification of the request
    HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("https://test.website.fr/Website/api/transactions/" + sVal + "/contrat"); //sVal is id for the webService
    wr.ContentType = "multipart/form-data; boundary=" + boundary;
    wr.Method = "POST";
    wr.KeepAlive = true;
    wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
    string sAuthorization = "login:password";//AUTHENTIFICATION BEGIN
    byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(sAuthorization);
    string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
    wr.Headers.Add("Authorization: Basic " + returnValue); //AUTHENTIFICATION END
    Stream rs = wr.GetRequestStream();
    
    
    string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; //For the POST's format
    
    //Writting of the file
    rs.Write(boundarybytes, 0, boundarybytes.Length);
    byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(Server.MapPath("questions.pdf"));
    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, "file", "questions.pdf", contentType);
    byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
    rs.Write(headerbytes, 0, headerbytes.Length);
    
    FileStream fileStream = new FileStream(Server.MapPath("questions.pdf"), 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();
    rs = null;
    
    WebResponse wresp = null;
    try
    {
        //Get the response
        wresp = wr.GetResponse();
        Stream stream2 = wresp.GetResponseStream();
        StreamReader reader2 = new StreamReader(stream2);
        string responseData = reader2.ReadToEnd();
    }
    catch (Exception ex)
    {
        string s = ex.Message;
    }
    finally
    {
        if (wresp != null)
        {
            wresp.Close();
            wresp = null;
        }
        wr = null;
    }
    

    【讨论】:

      【解决方案2】:

      这应该是你要找的:

      http://msdn.microsoft.com/en-us/library/debx8sh9.aspx

      这是一个快速的代码示例:

              byte[] pdfFile = File.ReadAllBytes("pdf file path here");
      
              WebRequest request = WebRequest.Create("https://test.site.fr/Testfile");
              request.Method = "POST";
              request.ContentLength = pdfFile.Length;
              request.ContentType = "application/pdf";
      
              Stream stream = request.GetRequestStream();
              stream.Write(pdfFile, 0, pdfFile.Length);
              stream.Close();
      
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
              StreamReader reader = new StreamReader(response.GetResponseStream());
              Console.WriteLine(reader.ReadToEnd());
              reader.Close();
      

      【讨论】:

      • 好的,谢谢
        但是,webService 需要更多信息:交易 id (id = 123456789)。供参考:如果我使用
        ,它可以工作,但是,如果我在 c# 中执行此操作(代码隐藏),它就不起作用。
        那么,我如何添加一个参数 (id=123465789)这段代码?
      • 这不是您最初要求的。听起来您要问的是您需要发送带有一些支持信息的pdf文档。服务器请求什么类型的数据?它需要 XML 吗?您应该重新措辞您的问题以包含您的全部问题。
      猜你喜欢
      • 2015-05-24
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 1970-01-01
      相关资源
      最近更新 更多