Web 请求在 Excel 加载项中的 Excel 启动时运行
我有一个 Excel 插件,当 Excel 启动时,它将访问一个 Web 服务 (GET),这是一个简单的 Web 服务请求,应该立即完成说:类似 https://mywebservice.com&application=myapp& user=currentuser
,结果是一个短(<200字节)JSON
表达式。
如果我在浏览器中运行请求,它会像预期的那样很快。
在我的 AddIn 中,我记录了 Web 请求从开始到结束的时间,通常(大约 40-50% 的时间)需要 3-5 秒,其他时候,它与从浏览器运行一样快。
当速度慢时,Excel没有任何反应,只需在状态栏中显示“Registering MyaddIn.xll...”。
我很困惑,不知道如何调试/解决问题。
谢谢
这是我用来调用 Web 服务的 C#
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;
}
I have an Excel AddIn, when Excel launches, it will access a web service (GET), it is a simple web service request and should finish immediately says: something like https://mywebservice.com&application=myapp&user=currentuser
, the result is a short (<200bytes) JSON
expression.
If I run the request in brower, it is real quick as expected.
In my AddIn, I recorded the time from start to end of the web request, frequently (about 40-50% of time) it takes 3-5 seconds, other times, it is real quick as run from brower.
When it is slow, Excel has no response, simply show "Registering MyaddIn.xll..." in status bar.
I am so confused and not sure how to debug/fix the issue.
thanks
Here is C# I use to call web service
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论