C#,webservice类型接口上传文件

postman
在这里插入图片描述
代码:

 //PDF转文件流
 string path="D:\\1.pdf";
 byte[] bytes = File.ReadAllBytes(path);
 List<CTGDFile> fileList = new List<CTGDFile>();
 CTGDFile ctfile = new CTGDFile();
 ctfile.fileDate = bytes;
 ctfile.name = "file";
 ctfile.filename = path;//这里是文件的路径
 fileList.Add(ctfile);
 string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
 request.ContentType = "multipart/form-data; boundary=" + boundary;
 request.Method = "POST";
 request.KeepAlive = true;
 //这里可以增加头部文件
 //request.Headers["x-timevale-project-id"] = Projectid;
 //request.Headers["x-timevale-signature"] = Secret;
 MemoryStream stream = new MemoryStream();
 byte[] line = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
 byte[] enterER = Encoding.ASCII.GetBytes("\r\n");
 提交文件
 if (fileList != null)
 {
     string fformat = "Content-Disposition:form-data; name=\"{0}\";filename=\"{1}\"\r\nContent-Type:{2}\r\n\r\n";
     foreach (CTGDFile file in fileList)
     {
         string contentType = "application/pdf";//上传文件的类型
         stream.Write(line, 0, line.Length);        //项目分隔符
         string s = string.Format(fformat, file.name, file.filename, contentType);
         WriteLog(s);
         byte[] data = Encoding.UTF8.GetBytes(s);
         stream.Write(data, 0, data.Length);
         stream.Write(file.fileDate, 0, file.fileDate.Length);
         stream.Write(enterER, 0, enterER.Length);  //添加\r\n
     }
 }
 byte[] foot_data = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");      //项目最后的分隔符字符串需要带上--  
 stream.Write(foot_data, 0, foot_data.Length);
 request.ContentLength = stream.Length;
 Stream requestStream = request.GetRequestStream(); //写入请求数据
 stream.Position = 0L;
 stream.CopyTo(requestStream); //
 stream.Close();
 requestStream.Close();
 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 return response;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: C# WebService 接口开发教程 1. 创建 WebService 应用程序 在 Visual Studio 中,选择“新建项目”,选择“ASP.NET Web 服务应用程序”,输入项目名称,点击“确定”按钮。 2. 添加 WebService 接口 在 Solution Explorer 中,右键单击项目名称,选择“添加新项”,选择“Web 服务”,输入 WebService 名称,点击“添加”按钮。 3. 编写 WebService 接口代码 在 WebService 文件中,编写 WebService 接口代码。例如: ``` [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class MyWebService : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } } ``` 4. 调试 WebService 接口 在 Visual Studio 中,点击“调试”菜单,选择“启动调试”,浏览器将打开 WebService 的测试页面。 5. 调用 WebService 接口 在客户端应用程序中,添加 WebService 引用。例如: ``` using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MyWebService; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { MyWebService.MyWebService ws = new MyWebService.MyWebService(); string result = ws.HelloWorld(); Response.Write(result); } } ``` 以上就是 C# WebService 接口开发的基本流程。 ### 回答2: 很抱歉,您的问题不明确。请提供更多信息以便我能够回答您的问题。 ### 回答3: 很抱歉,由于缺乏上下文,我无法理解和回答“c”这个问题,请您尽可能提供更多的信息和背景。感谢您的理解。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Except!!!

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值