I am having an issue in someone else’s code. I have code in one project that calls a dll that contains the code in question.
The code I’m calling is below:
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);
}
}
As youc an see I have put in a lot of error handling in their code to get to the source of the error. From this I get this output:
<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>
Any help would be appreciated.
Thanks
Sachin
You can use the UploadString as
and track the exception as it is returned by the server. Also you have to ensure that the server is not waiting for a post. If yes then use