繁体   English   中英

WebException调用dll时

[英]WebException When calling dll

我在别人的代码中有问题。 我在一个项目中调用了包含所讨论代码的dll的代码。

我正在调用的代码如下:

public void SaveCustomTemplate(string templateName, string templateValue)
        {
            var action = "SaveCustom";
            var webCient = new WebClient();
            var address = String.Format(urlFormat, rootUrl, action);
            var queryString = new NameValueCollection();
            queryString.Add("id",templateName);
            queryString.Add("key",apiKey);
            queryString.Add("html",templateValue);
            webCient.QueryString = queryString;

            try
            {
                webCient.UploadString(address,templateValue);
                templatesCache[templateName] = templateValue;
            }
            catch (WebException e)
            {
                var msg = String.Format("Cannot save the template '{0}'. Message:{1}.  InMessage:{2}.  Response:{3}.  Source:{4}.  Stack:{5}.  Status:{6}.  TargetSite:{7}", address, e.Message, e.InnerException.Message, e.Response, e.Source, e.StackTrace, e.Status, e.TargetSite);
                throw new WebException(msg);
            }
        }

如您所见,我在他们的代码中加入了很多错误处理以获取错误源。 由此我得到以下输出:

<div class="alert success" id="SuccessMessages">Cannot save the template 'http://localhost:30001/Templates/SaveCustom'. Message:The underlying connection was closed: An unexpected error occurred on a receive..  InMessage:Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..  Response:.  Source:System.  Stack:   at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
   at System.Net.WebClient.UploadString(Uri address, String method, String data)
   at System.Net.WebClient.UploadString(String address, String data)
   at EmailServices.TemplateLoaders.HttpTemplateLoader.SaveCustomTemplate(String templateName, String templateValue) in C:\Projects\Red2Framework\Red2Framework\EmailServices\TemplateLoaders\HttpTemplateLoader.cs:line 94.  Status:ReceiveFailure.  TargetSite:Byte[] UploadDataInternal(System.Uri, System.String, Byte[], System.Net.WebRequest ByRef)</div>

任何帮助,将不胜感激。

谢谢

萨钦

您可以将UploadString用作

reply = webCient.UploadString(address, templateValue);

并跟踪服务器返回的异常。 另外,您还必须确保服务器不等待发布。 如果是,则使用

reply = webCient.UploadString(address, "POST", templateValue);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM