简体   繁体   中英

Test ASMX webservice

I wrote a simple webservice for uploading a file.

<%@ 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";
    }

}

Now I am trying to test the functionality but am having trouble interacting with the webservice via C#. I've searched around tried using HTTPWebrequest (multipart/form-data) that I found in this post but didn't have much success and am not sure that this is the right approach.

How can I test the webservice that I wrote to see if I can successfully upload a file?

Are you looking to write test cases or just run some tests via curl or a ui

Could use WCF Test Client Could use curl

Here is a link to some code which should help as well.

One easy to test the code is to create unit test by right clicking on method you want to test and select Create Unit Tests. You will have a test method stub generated for you with all required variables initialized to null. Initialize all the variables with the data you want and run the unit tests. This will test the method itself.
I am not sure if this is what you are looking for.

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