简体   繁体   English

如何在C#的Web服务中发送多部分/表单数据?

[英]how to send multipart/form-data in web services that c#?

how to read multipart/form-data in web services? 如何读取Web服务中的多部分/表单数据? i am send data using postman body form-data but postman is getting error. 我使用邮递员的身体形式数据发送数据,但邮递员遇到错误。

    public class Api : System.Web.Services.WebService
    {
        [WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
        public string Hell(string name)
        {
            return CommonUtilities.GetJSonSerialized(name);
        }
    }

error System.InvalidOperationException: Request format is invalid: multipart/form-data; 错误System.InvalidOperationException:请求格式无效:multipart / form-data; boundary=----WebKitFormBoundary79Ky1A1Kfyyy7qUi. boundary = ---- WebKitFormBoundary79Ky1A1Kfyyy7qUi。 at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()处的System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()处

that error have getting by postman. 该错误由邮递员得到。

enter image description here 在此处输入图片说明

Asmx file can also be used for rest api creation (Which is not the recommended approach). Asmx文件也可以用于创建REST API(不推荐使用这种方法)。

This can be achieved by the below code snippet. 这可以通过下面的代码片段实现。

[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Randezvous : WebService
{
    [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public void getUnitPersonels(string user, string pass, decimal unitNo)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        #region ..:: Kullanıcı şİfre Kontrol ::..
        if (!(unit == "xxx" && pass == "yyy"))
        {

            string msg = "User or pass is wrong.";
            Context.Response.Write(serializer.Serialize(msg));
            return;
        }
        #endregion

        List<Personels> personels = _units.getUnitPersonels(unitNo);

        string jsonString = serializer.Serialize(personels);
        Context.Response.Write(jsonString);
    }
}

You can test this code in c# with the code that is shown below: 您可以使用下面显示的代码在c#中测试此代码:

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var builder = new UriBuilder("http://localhost:18511/Randezvous.asmx/getUnitPersonels");
    var query = HttpUtility.ParseQueryString(builder.Query);
    query["unitNo"] = "0";
    builder.Query = query.ToString();
    string url = builder.ToString();

    var result = Task.FromResult(client.GetAsync(url).Result).Result.Content;
    var resultJson = result.ReadAsStringAsync().Result;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM