Http Reuse of connections

  • Thread starter Thread starter want 2 Learn
  • Start date Start date
W

want 2 Learn

Guest
I look for a way to reuse connection and work in async at the same time.

first I loop over the data I get from the db :

foreach (SmsMessage message in lSmsMessage)
{
HttpManager httpManager = new HttpManager();
inputTaskList.Add(httpManager.SendData(strURL, message, Encoding.Default,xml);
}

var results = await Task.WhenAll(inputTaskList);

and the HttpManager class :

public class HttpManager
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
private readonly static string reservedCharacters = "!*'();:@&=+$,/?%#[]";
public async Task<bool> SendData(string url, Message message, Encoding encodingPage,string baseXml)
{
string xml = BuildXml(message, baseXml);
bool isSuccess = true;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.ConnectionClose = false;//keeyp-alive
string resultContent = string.Empty;

client.BaseAddress = new Uri(url);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("XML", xml)
});
try
{
HttpResponseMessage result = await client.PostAsync("", content);
if (result.StatusCode == HttpStatusCode.OK)
{
resultContent = await result.Content.ReadAsStringAsync();
if(resultContent.IndexOf(">True<")>0)
{
}
else
{
isSuccess = false;
logger.Error("Failes to send1,reason={0}", resultContent);
}

}
else
{
isSuccess = false;
logger.Error("Failes to send2 CampaignId={3}");
}
}
catch (Exception ex)
{

logger.Error("Failes to send3()::failed sending Err={0}", ex.Message);
}

}

return isSuccess;
}



private static string ReplaceSpeicalChars(string txt)
{
txt=txt.Replace("&", "&amp;");
return txt;
}

}


what is the corret wa to resuse the connections?

Continue reading...
 
Back
Top