C# 调用dll时出现WebException
我在其他人的代码中遇到问题。我有一个项目中的代码,它调用了一个包含有问题代码的dll 我调用的代码如下:C# 调用dll时出现WebException,c#,wcf,C#,Wcf,我在其他人的代码中遇到问题。我有一个项目中的代码,它调用了一个包含有问题代码的dll 我调用的代码如下: public void SaveCustomTemplate(string templateName, string templateValue) { var action = "SaveCustom"; var webCient = new WebClient(); var address = Stri
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>
无法保存模板的http://localhost:30001/Templates/SaveCustom'. 消息:基础连接已关闭:接收时发生意外错误。。InMessage:无法从传输连接读取数据:远程主机强制关闭了现有连接。。答复:。资料来源:系统。堆栈:位于System.Net.WebClient.UploadDataInternal(Uri地址、字符串方法、字节[]数据、WebRequest和request)
位于System.Net.WebClient.UploadString(Uri地址、字符串方法、字符串数据)
位于System.Net.WebClient.UploadString(字符串地址、字符串数据)
在C:\Projects\Red2Framework\Red2Framework\EmailServices\TemplateLoaders\HttpTemplateLoader.SaveCustomTemplate(字符串templateName,字符串templateValue)中的EmailServices.TemplateLoader.HttpTemplateLoader.cs:第94行。状态:接收失败。TargetSite:Byte[]UploadDataInternal(System.Uri,System.String,Byte[],System.Net.WebRequest ByRef)
任何帮助都将不胜感激
谢谢
Sachin您可以将上载字符串用作
reply = webCient.UploadString(address, templateValue);
并跟踪服务器返回的异常。您还必须确保服务器没有等待post。如果是,则使用
reply = webCient.UploadString(address, "POST", templateValue);
我将“post”操作放入uploadstring方法调用中,并打印出回复,结果是没有差异,回复为空。然后,如果服务器脚本中没有bug,可能出现的情况,服务器需要的是数据而不是字符串,或者字符串包含非ascii字符,并且由于webclient.encoding未定义,因此无法正常工作。我尝试使用UploadString方法的异步版本,但到目前为止它似乎可以正常工作。问题是它(旧的非异步版本)有时工作,有时不工作,所以我不认为服务器需要数据而不是字符串。太好了。您解决了它,但现在您必须找到原因(可选)