如何使用HttpWeRequest将XML字符串发布到具有参数类型HttpPostedFileBase的MVC控制器

时间:2017-03-23 06:52:26

标签: c# xml asp.net-mvc-4 file-upload xmlhttprequest

我想对我的API进行性能测试,用于使用HttpWebRequest上传XML文件。

MVC控制器

[HttpPost]
     public bool UploadFile(HttpPostedFileBase file) {
        if(file!=null){
         ///do something
         }
        }

网络请求代码

    string XMLstring = <Details><Name>ABC</Name><City>Mumbai</City></Details>;
string URL = "localhost:1010/ControllerName/UploadFile"   
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
    request.ContentType = "application/x-www-form-urlencoded";
    request.Method = "POST";
    StringBuilder stringBuilder = new StringBuilder();
                try
                {
                    byte[] postData = Encoding.UTF8.GetBytes(XMLstring);

                    request.ContentLength = postData.Length;
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(postData, 0, postData.Length);
                    }
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {

                    }
                }
                catch (Exception ex)
                {
                }

因此调用了Above API,但它在参数中显示为null值。

0 个答案:

没有答案