MVC4 web api post RestRequest with XML string in URL



我有一个看起来像这样的ApiController:

public class UploadController : ApiController
{
    public StatusModel PostXML(string languageCode, string username, string password, string xml)
    {
        ...
    }
}

我正在尝试从外部项目调用此方法,如下所示:

public StatusModel UploadXML() {
    var client = new RestClient("");
    string url = "http://localhost:52335/api/upload/PostXML/de/" + HttpUtility.UrlEncode(TESTXML) + "/user/password";
    var request = new RestRequest(url, Method.POST);
    return client.Execute<StatusModel>(request).Data;
}

当 TESTXML 变量是像"Test"这样的简单文本时,Web api 方法会被调用并传输值,但是一旦我在其中放置任何 xml 标记,即使它只是一个"<",它不再尽管我的 UrlEncoding 。不仅没有调用我的 web api 函数,调用我的 UploadXML 方法的 Ajax 会跳入错误函数,尽管得到了 http 响应 200。

经过几个小时的尝试寻找解决方案,我没主意了。我做错了什么?如何将 XML 字符串作为参数传递到 URL 中?

谢谢

你在这里有很多问题,但一般来说你应该发布你的数据,而不是使用 GET url,来创建帖子: domain.com/controller/action/some/data/here 是一个获取请求。 domain.com/controller/action 是您的帖子 URL,数据在帖子 URL 表单参数中。然后,您将操作绑定到模型,而不是像您所做的那样绑定到单个参数。

以下是我在基类中使用的一些代码,用于从服务器端代码调用 API 时进行所有 API 调用:

public string CreateApiRequest(string url, object model, bool isPost)
    {
        try
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            string postData = "";
            if(model != null)
            {
                postData = Strings.SerializeToQueryString(model).TrimEnd(Convert.ToChar("&"));
            }
            byte[] data = encoding.GetBytes(postData);
            var serviceUrl = "http://" + HttpContext.Request.Url.Host + "/api/{0}";
            // create the post and get back our data stream
            var myRequest = (HttpWebRequest)WebRequest.Create(new Uri(string.Format(serviceUrl, url)));
            if (isPost)
            {
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.Accept = "application/json";
                myRequest.ContentLength = data.Length;
                Stream newStream = myRequest.GetRequestStream();
                // Send the data.
                newStream.Write(data, 0, data.Length);
                newStream.Close();
            }
            else
            {
                myRequest.Method = "GET";
                myRequest.Accept = "application/json";
            }

            // Get response  
            using (HttpWebResponse response = myRequest.GetResponse() as HttpWebResponse)
            {
                // Get the response stream  
                var reader = new StreamReader(response.GetResponseStream());
                // Read the whole contents and return as a string  
                var myString = reader.ReadToEnd();
                return myString;
            }
        }
        catch (Exception ex)
        {
            // handle error here, I have my own custom mailer
            throw;
        }
    }

这就是我所说的。

var model = JsonConvert.DeserializeObject<List<Address>>(CreateApiRequest(url: "Address", model: null, isPost: false));

希望这有帮助

最新更新