Reputation: 111
I am iterating one foreach loop
in which I am parsing one string like this:
I want to fire parallel request in HttpWebRequest
for my string "imageUrl
". Means At a time 4 request and response type of system.(For Utility of my Core).
How can I do it?
Code:
foreach (var item in requestData)
{
try
{
string imageUrl = Convert.ToString(item.ImageUrl);
imageUrl = imageUrl.Replace('\r', ' ');
//It is Use for saving at our project location.
string saveLocation =
@AppDomain.CurrentDomain.BaseDirectory + @"Image\someone.jpg";
byte[] imageBytes;
try
{
HttpWebRequest imageRequest =
(HttpWebRequest)WebRequest.Create(imageUrl);
WebResponse imageResponse = imageRequest.GetResponse();
Stream responseStream = imageResponse.GetResponseStream();
using (BinaryReader br = new BinaryReader(responseStream))
{
imageBytes = br.ReadBytes(500000);
br.Close();
}
responseStream.Close();
imageResponse.Close();
FileStream fs = null;
BinaryWriter bw = null;
fs = new FileStream(saveLocation, FileMode.Create);
bw = new BinaryWriter(fs);
bw.Write(imageBytes);
fs.Close();
bw.Close();
id = Convert.ToString(item.Id);
// string ImageUrl= Convert.ToString(dr["ImageUrl"]);
ImageProcess(id);
}
catch (Exception)
{
log.Info("Image Url Wrong!!!");
drCurrentRow = dt.NewRow();
drCurrentRow["Id"] = Convert.ToString(item.Id); ;
drCurrentRow["Label"] = "Error";
drCurrentRow["Score"] = 0;
drCurrentRow["Flag"] = "1";
dt.Rows.Add(drCurrentRow);
//drCurrentRow = dt.NewRow();
dt.AcceptChanges();
}
finally
{
fs.Close();
bw.Close();
}
}
catch (Exception e)
{
log.Info(e);
}
}
BulkInsert();
}
I want to fire parallel request in HttpWebRequest
for my string imageUrl
.
How can I do it?
Upvotes: 0
Views: 148
Reputation: 86
You could use Parallel.ForEach() or by Using async and await
Parallel.ForEarch(requestData, item => {
//web request
});
Upvotes: 0