下面代码中标记的行(//这是异步调用)。如果我将此值作为查询传递给我的函数,则不会调用异步回调:
<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);
}
}