为什么我得到一个&QUOT

为什么我得到一个&QUOT

本文介绍了为什么我得到一个"引发NotSupportedException"与此code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打电话从手持设备(Compact Framework的)的Web API方法与此code:

I am trying to call a Web API method from a handheld device (Compact Framework) with this code:

// "fullFilePath" is a value such as "\Program Files\Bla\abc.xml"
// "uri" is something like "http://localhost:28642/api/ControllerName/PostArgsAndXMLFile?serialNum=8675309&siteNum=42"
SendXMLFile(fullFilePath, uri, 500);
. . .
public static string SendXMLFile(string xmlFilepath, string uri, int timeout)
{
    uri = uri.Replace('\\', '/');
    if (!uri.StartsWith("/"))
    {
        uri = "/" + uri;
    }
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;

    request.Method = "POST";

    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
        byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());

        if (timeout < 0)
        {
            request.ReadWriteTimeout = timeout;
            request.Timeout = timeout;
        }

        request.ContentLength = postBytes.Length;
        request.KeepAlive = false;

        request.ContentType = "application/x-www-form-urlencoded"; // not "text/xml" correct?

        try
        {
            Stream requestStream = request.GetRequestStream();

            requestStream.Write(postBytes, 0, postBytes.Length);
            requestStream.Close();

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                return response.ToString();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            request.Abort();
            return string.Empty;
        }
    }
}

在某处的 SendXMLFile()的,它与失败的引发NotSupportedException 虽然...由于这是一个手持设备上运行,我不能把一个在断点它并通过它一步;我撒了一堆调试语句的整个(MessageBox.Show()),但我宁愿不这么做。

Somewhere in SendXMLFile(), it is failing with "NotSupportedException" though... As it's running on a handheld device, I can't put a breakpoint in it and step through it; I could sprinkle a bunch of debug statements throughout (MessageBox.Show()), but I'd rather not do that.

服务器code甚至从来没有达到我穿上断点的XDocument DOC =下面一行:

The server code never even reaches the breakpoint I put on the "XDocument doc =" line below:

[Route("api/ControllerName/PostArgsAndXMLFile")]
public void PostArgsAndFile([FromBody] string stringifiedXML, string serialNum, string siteNum)
{
    XDocument doc = XDocument.Parse(stringifiedXML);

难道Compact Framework中不能调用(REST风格)的Web API由于某种原因的方法?显然,客户端(手持/ Compact Framework的),编译和运行,它只是拒绝实际上它的所有运行的现实跟进。

Is it that the Compact framework can't call a (RESTful) Web API method for some reason? Obviously, the client (handheld/Compact Framework) compiles and runs, it just refuses to actually follow through with the runtime realities of it all.

我的code要求小改动它适合,或者我需要采取完全不同的策略?

Does my code require a small alteration for it to fit, or do I need to take a completely different tack?

推荐答案

网页API是不会能够处理您的主体内容。你宣布它为应用程序/ x外形urlen codeD ,但它实际上是XML格式的,你的方法签名是希望它是一个XMLDataContract连载字符串

Web API is not going to be able to handle your body content. You declared it as application/x-form-urlencoded, but it is actually XML formatted and your method signature is expecting it to be a XMLDataContract serialized string.

而不是使用参数 stringifiedXML ,而不是,只是读了你的身体里面的方法..

Instead of using the parameter stringifiedXML, instead, just read the body inside your method..

[Route("api/ControllerName/PostArgsAndXMLFile")]
public async void PostArgsAndFile(string serialNum, string siteNum)
{
    XDocument doc = XDocument.Parse(await Request.Content.ReadAsStringAsync());
}

或事件更好,直接使用流。

Or event better, use a stream directly.

[Route("api/ControllerName/PostArgsAndXMLFile")]
public async void PostArgsAndFile(string serialNum, string siteNum)
{
    XDocument doc = XDocument.Load(await Request.Content.ReadAsStreamAsync());
}

这样的话,你可以把contentType中在客户端上回应用程序/ XML 理所应当的。

这篇关于为什么我得到一个&QUOT;引发NotSupportedException&QUOT;与此code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:54