【发布时间】:2014-03-07 20:01:28
【问题描述】:
我正在尝试使用以下代码从手持设备(Compact Framework)调用 Web API 方法:
// "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()) 中添加一堆调试语句,但我宁愿不这样做。
服务器代码甚至没有到达我在下面的“XDocument doc =”行中设置的断点:
[Route("api/ControllerName/PostArgsAndXMLFile")]
public void PostArgsAndFile([FromBody] string stringifiedXML, string serialNum, string siteNum)
{
XDocument doc = XDocument.Parse(stringifiedXML);
是不是因为某种原因,Compact 框架不能调用(RESTful)Web API 方法?显然,客户端(手持式/紧凑型框架)编译并运行,它只是拒绝真正遵循所有的运行时现实。
我的代码是否需要稍作改动才能适应,还是我需要采取完全不同的策略?
【问题讨论】:
-
尚未审查您的代码,但马上开始...1。为什么你不能通过设备上的代码来学习?我一直这样做。 2. Fiddler 在请求期间在服务器端运行时会告诉你什么?请求是否通过?
-
如果
uri is something like http://localhost:28642/api/ControllerName/PostArgsAndXMLFile?serialNum=8675309&siteNum=42然后你执行uri = "/" + uri;,这不会产生格式错误的URL吗? -
@tcarvin: "服务器代码甚至没有到达断点"
-
@tcarvin:“通过设备上的代码steo?我一直这样做”我对此的唯一回应是:“Luck-E!”几个月前,我尝试这样做的辛苦在这个网站上得到了很好的记录,但我从来没有能够做到这一点。我可以在服务器应用程序中设置断点,但不能在手持设备上。
-
不记得那个特定的线程,但我对无法在设备上进行调试表示哀悼。
标签: asp.net-web-api httpwebrequest compact-framework bytearray streamreader