C# 在Excel加载项中的Excel启动时运行web请求
我有一个Excel插件,当Excel启动时,它将访问一个web服务GET,这是一个简单的web服务请求,应该立即完成,并说:类似https://mywebservice.com&application=myapp&user=currentuser,结果是一个简短的Excel加载项事件,您使用哪个事件调用此下载方法?我正在使用Excel DNA对我的加载项进行编码。这不是Excel事件。它处于自动打开状态。它在Excel启动时运行。这是在不同的系统/登录名上发生的,还是在同一个系统+登录名上发生的?那么我怀疑是网络问题还是加载项代码中存在争用情况。。。你有详细的日志吗?C# 在Excel加载项中的Excel启动时运行web请求,c#,excel,add-in,xll,excel-dna,C#,Excel,Add In,Xll,Excel Dna,我有一个Excel插件,当Excel启动时,它将访问一个web服务GET,这是一个简单的web服务请求,应该立即完成,并说:类似https://mywebservice.com&application=myapp&user=currentuser,结果是一个简短的Excel加载项事件,您使用哪个事件调用此下载方法?我正在使用Excel DNA对我的加载项进行编码。这不是Excel事件。它处于自动打开状态。它在Excel启动时运行。这是在不同的系统/登录名上发生的,还是在同一个系统+登录名上发生的
private static int DownloadInfoFromServer(string entUrl, string localFilename)
{
// Function will return the number of bytes processed
// to the caller. Initialize to 0 here.
int bytesProcessed = 0;
// Assign values to these objects here so that they can
// be referenced in the finally block
Stream remoteStream = null;
Stream localStream = null;
HttpWebResponse response = null;
HttpWebRequest request;
// Use a try/catch/finally block as both the WebRequest and Stream
// classes throw exceptions upon error
try
{
//clear out local file every time no matter request fails or not
localStream = File.Create(localFilename);
request = ServiceBase.GetHttpWebRequestWithProxyForEnt(entUrl);
response = (HttpWebResponse)request.GetResponse();
// Once the WebResponse object has been retrieved,
// get the stream object associated with the response's data
remoteStream = response.GetResponseStream();
if (remoteStream != null)
{
// Allocate a 1k buffer
var buffer = new byte[1024];
int bytesRead;
// Simple do/while loop to read from stream until
// no bytes are returned
do
{
// Read data (up to 1k) from the stream
bytesRead = remoteStream.Read(buffer, 0, buffer.Length);
// Write the data to the local file
localStream.Write(buffer, 0, bytesRead);
// Increment total bytes processed
bytesProcessed += bytesRead;
} while (bytesRead > 0);
}
}
catch (Exception e)
{
Helper.LogError(e);
}
finally
{
if (response != null) response.Close();
if (remoteStream != null) remoteStream.Close();
if (localStream != null) localStream.Close();
}
// Return total bytes processed to caller.
return bytesProcessed;
}