我写了一个简单的网络服务来上传文件。
<%@ WebService Language="C#" class="AppWebService" %>
using System;
using System.Web.Services;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.IO;
[WebService(Namespace="http://myip/services")]
public class AppWebService : WebService
{
[WebMethod]
public string UploadFile(byte[] f, string fileName)
{
// the byte array argument contains the content of the file
// the string argument contains the name and extension
// of the file passed in the byte array
try
{
// instance a memory stream and pass the
// byte array to its constructor
MemoryStream ms = new MemoryStream(f);
// instance a filestream pointing to the
// storage folder, use the original file name
// to name the resulting file
FileStream fs = new FileStream
(System.Web.Hosting.HostingEnvironment.MapPath("/TransientStorage/") +
fileName, FileMode.Create);
// write the memory stream containing the original
// file as a byte array to the filestream
ms.WriteTo(fs);
// clean up
ms.Close();
fs.Close();
fs.Dispose();
// return OK if we made it this far
return "OK";
}
catch (Exception ex)
{
// return the error message if the operation fails
return ex.Message.ToString();
}
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
现在我正在尝试测试功能,但无法通过 C# 与 web 服务交互。我已经尝试使用HTTPWebrequest (multipart/form-data)
我在这篇文章中找到的方法进行搜索,但没有取得太大的成功,并且不确定这是正确的方法。
如何测试我编写的 web 服务以查看是否可以成功上传文件?