0

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?

ρяσѕρєя K
133k54 gold badges203 silver badges216 bronze badges
asked Jan 30, 2017 at 8:28
1

2 Answers 2

1
Parallel.ForEach(requestData, item =>
 {
 //your code 
 });
answered Feb 3, 2017 at 5:48
0

You could use Parallel.ForEach() or by Using async and await

Parallel.ForEarch(requestData, item => {
 //web request
});
answered Feb 3, 2017 at 6:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.