C# 递归HTTP调用在IDE和部署的可执行文件中表现出不同的行为
代码正在对SVN树的公开表示进行HTTP调用。然后,它将解析HTML并添加文件,以供以后下拉并推送给用户参考。这是在WPF应用程序中完成的。下面是显示目录结构的代码和图像C# 递归HTTP调用在IDE和部署的可执行文件中表现出不同的行为,c#,.net,wpf,http,recursion,C#,.net,Wpf,Http,Recursion,代码正在对SVN树的公开表示进行HTTP调用。然后,它将解析HTML并添加文件,以供以后下拉并推送给用户参考。这是在WPF应用程序中完成的。下面是显示目录结构的代码和图像 private readonly String _baseScriptURL = @"https://xxxxxxxxxx/svn/repos/xxxxxxxxxx/trunk/scripts/vbs/web/"; private void FindScripts(String url, ref IColle
private readonly String _baseScriptURL = @"https://xxxxxxxxxx/svn/repos/xxxxxxxxxx/trunk/scripts/vbs/web/";
private void FindScripts(String url, ref ICollection<String> files)
{
//MyFauxMethod();
StringBuilder output = new StringBuilder();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = new Credentials().GetCredentialCache(url);
_logger.Log("Initiating request [" + url + "]", EventType.Debug);
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
{
_logger.Log("Response received for request [" + url + "]", EventType.Debug);
int count = 0;
byte[] buffer = new byte[256];
while ((count = stream.Read(buffer, 0, buffer.Length)) > 0)
{
if (count < 256)
{
List<byte> trimmedBuffer = buffer.ToList();
trimmedBuffer.RemoveRange(count, 256 - count);
String data = Encoding.ASCII.GetString(trimmedBuffer.ToArray());
output.Append(data);
}
else
{
String data = Encoding.ASCII.GetString(buffer);
output.Append(data);
}
}
}
String html = output.ToString();
HTMLDocument doc = new HTMLDocumentClass();
IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
doc2.write(new object[] { html });
IHTMLElementCollection ul = doc.getElementsByTagName("li");
doc2.close();
doc.close();
foreach (IHTMLElement item in ul)
{
if (item != null &&
item.innerText != null)
{
String element = item.innerText.Trim().Replace(" ", "%20");
//nothing to do with going up a dir
if (element == "..")
continue;
_logger.Log("Interrogating [" + element + "]", EventType.Debug);
String filename = System.IO.Path.GetFileName(element);
if (String.IsNullOrEmpty(filename))
{
//must be a directory; recursively search if honored dir
if (!_ignoredDirectories.Contains(element))
{
_logger.Log("Searching directory [" + element + "]", EventType.Debug);
FindScripts(url + System.IO.Path.GetDirectoryName(element) + "/", ref files);
}
else
_logger.Log("Ignoring directory [" + element + "]", EventType.Debug);
}
else
{
//add honored files to list for parsing meta data later
if (_honoredExtensions.Contains(System.IO.Path.GetExtension(filename)))
{
files.Add(url + filename);
_logger.Log("Added file [" + (url + filename) + "]", EventType.Debug);
}
}
//MyFauxMethod();
}
//MyFauxMethod();
}
}
catch (Exception e)
{
_logger.Log(e);
}
//MyFauxMethod();
}
private void MyFauxMethod()
{
int one = 1;
int two = 2;
int three = one + two;
}
private只读字符串\u baseScriptURL=@”https://xxxxxxxxxx/svn/repos/xxxxxxxxxx/trunk/scripts/vbs/web/";
私有void findscript(字符串url、引用ICollection文件)
{
//MyFauxMethod();
StringBuilder输出=新的StringBuilder();
HttpWebRequest请求=(HttpWebRequest)WebRequest.Create(url);
request.Credentials=new Credentials().GetCredentialCache(url);
_logger.Log(“启动请求[“+url+”]”,EventType.Debug);
尝试
{
使用(HttpWebResponse=(HttpWebResponse)request.GetResponse())
使用(Stream=response.GetResponseStream())
{
_logger.Log(“收到请求[“+url+”]”的响应,EventType.Debug);
整数计数=0;
字节[]缓冲区=新字节[256];
而((count=stream.Read(buffer,0,buffer.Length))>0)
{
如果(计数<256)
{
List trimmedBuffer=buffer.ToList();
trimmedBuffer.RemoveRange(计数,256-计数);
字符串数据=Encoding.ASCII.GetString(trimmedBuffer.ToArray());
输出。追加(数据);
}
其他的
{
字符串数据=Encoding.ASCII.GetString(缓冲区);
输出。追加(数据);
}
}
}
字符串html=output.ToString();
HTMLDocument doc=新的HTMLDocumentClass();
IHTMLDocument2doc2=(IHTMLDocument2)doc;
doc2.write(新对象[]{html});
IHTMlementCollection ul=doc.getElementsByTagName(“li”);
doc2.close();
doc.close();
foreach(ul中的IHTMlement项)
{
如果(项!=null&&
item.innerText!=null)
{
String元素=item.innerText.Trim().Replace(“,“%20”);
//这和升职无关
如果(元素=“.”)
继续;
_logger.Log(“查询[“+元素+”]”,EventType.Debug);
字符串文件名=System.IO.Path.GetFileName(元素);
if(String.IsNullOrEmpty(文件名))
{
//必须是目录;如果是,则递归搜索
if(!\u ignoredDirectories.Contains(元素))
{
_Log(“搜索目录[“+元素+”]”,EventType.Debug);
FindScript(url+System.IO.Path.GetDirectoryName(元素)+“/”,ref文件);
}
其他的
_Log(“忽略目录[“+元素+”]”,EventType.Debug);
}
其他的
{
//将旧文件添加到列表中,以便稍后解析元数据
if(_honoredExtensions.Contains(System.IO.Path.GetExtension(filename)))
{
添加(url+文件名);
_logger.Log(“添加的文件[”+(url+文件名)+“]”,EventType.Debug);
}
}
//MyFauxMethod();
}
//MyFauxMethod();
}
}
捕获(例外e)
{
_logger.Log(e);
}
//MyFauxMethod();
}
私有void MyFauxMethod()
{
int-one=1;
int 2=2;
int三=一+二;
}
首先为冗长的代码块道歉;然而,我想确定完整的方法是否被理解。存在的问题仅适用于在IDE之外使用生成的发布版可执行文件时。如果版本构建是在IDE中运行的,那么它可以正常运行
此外,在IDE外部或IDE内部执行生成的调试构建时不存在问题;它在这两种情况下都能正常工作
问题是递归调用会阻止代码继续通过递归方法。线程中没有抛出异常;在移动到每个目录之前,一切都会停止,就像在其他构建中一样
发布版构建版的日志行如下所示
发起请求
[收到对请求的答复 [
询问[beq/]
搜索 目录[beq/]
要求 [
收到对请求的答复 [
询问[core/]
搜索 目录[core/]
要求 [
收到对请求的答复 [
审问 [BEQ核心%20Library.vbs]
已添加 文件 [
询问[一次性]
搜索目录[一次性]
发起请求 [
收到对请求的答复 [
递归查找脚本花费了[6]秒 [140]毫秒用于[1]
分析元数据 [1]用了[0]米[0]秒[906]毫秒
总时间为[0]米[7]秒[46]毫秒
更新: 在调试期间添加了大约3条额外的日志行之后,它现在可以正常工作了。悬而未决的问题是为什么?尝试在单独的应用程序中隔离问题代码会产生错误
GC.Collect()
GC.WaitForPendingFinalizers()