等号数据“导致”BeginGetRequest流失败
本文关键字:BeginGetRequest 流失 失败 导致 数据 | 更新日期: 2023-09-27 18:32:42
下面代码中标记的行(//这是异步调用)。 如果我将此值作为查询传递给我的函数,则不会回调异步回调:
<Verb>get</Verb><ResourceList><CatalogQuery><ItemQueryList><ItemIDList><ID>16-=-cedar-bonsai</ID></ItemIDList><AttributesType>all</AttributesType></ItemQueryList></CatalogQuery>
请注意 xml 的这一部分:
<ID>16-=-cedar-bonsai</ID>
但是,如果我将等号切换到其他任何东西,它可以工作
<Verb>get</Verb><ResourceList><CatalogQuery><ItemQueryList><ItemIDList><ID>16-here-cedar-bonsai</ID></ItemIDList><AttributesType>all</AttributesType></ItemQueryList></CatalogQuery>
真正让我感到困惑的是,查询的内容应该对被调用的回调绝对没有影响。右?
这是代码
//Get get a item attributes Async
public XElement makeAsyncCall(string query,bool create = false,bool resent = false)
{
ManualResetEvent mre = null;
//The results will be stored here;
StringBuilder xresults = new StringBuilder() ;
sem.WaitOne();
try
{
query = query.Replace("&", "&");
query = query.Replace("#39;", "apos;");
//If this is not a query that is being sent again append additional info
if (!resent)
{
query = HEADER_TEXT + query + CLOSER_TEXT;
}
//Concatonate a declaration, header information, the query to be performed, and the closing XML tag. Then convert to a Byte array
Byte[] byteData = UTF8Encoding.UTF8.GetBytes(
new XDocument(
new XDeclaration("1.0", "utf-8", null),
XElement.Parse(query)
).ToString()
);
string paddr = POST_ADDRESS_QUERY;
if (create != false)
{
paddr = POST_ADDRESS_CREATION;
}
//Setup the post connection
HttpWebRequest httpWReq = WebRequest.Create(paddr) as HttpWebRequest;
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.Proxy = null;
httpWReq.ContentLength = byteData.Length;
httpWReq.KeepAlive = true;
using (mre = new ManualResetEvent(false))
{
//THIS IS THE ASYNC CALL
httpWReq.BeginGetRequestStream(new AsyncCallback(getRequestStreamAsync), Tuple.Create<HttpWebRequest, byte[], bool, StringBuilder, ManualResetEvent>(httpWReq, byteData, create, xresults, mre));
mre.WaitOne(10000);
mre.Close();
//Try again if not exited already.
if (xresults.Length == 0)
{
httpWReq.Abort();
sem.Release();
return makeAsyncCall(query, create, true);
}
sem.Release();
return XElement.Parse(xresults.ToString(), LoadOptions.PreserveWhitespace);
}
}
catch(Exception ex)
{
mre.Close();
sem.Release();
throw (ex);
}
}
我找到了导致问题的原因。正在执行回调,但它不会导致断点停止。今天早上我再次尝试代码,并捕获了断点。我不确定这是视觉工作室中的错误还是未定义的行为?也许其他人在这方面对Visual Studio有更多的了解。
请求通过良好并收到响应,但后来在我的代码中,我生成了自己的异常,捕获它们,抛出它们,然后重试命令。所以它陷入了一个无限循环。