How to Handle HTTP Post Call inside for loop

Posted by Gow.Net under Windows Forms on 4/30/2014 | Points: 10 | Views : 848 | Status : [Member] | Replies : 2
Hi All

Bellow i display my code Here i have one array i need to send array value one by one to api call post method.It's working fine.
But if array have 100 value means while loop executing my windows application was hanged.So please any one give some idea are solution for handle this.


 for(int i=0;i<array.lrength;i++){
sendValue(array[i]);
}

public void sendValue(string tag)
{
string post_data = "tag=";
post_data += tag;
post_data += "&key=test1234";
string uri = localHost:3000/api";
try
{
// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(uri);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version11;
request.Method = "POST";

byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

// this is important - make sure you specify type this way
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();

requestStream.Write(postBytes, 0, postBytes.Length);

requestStream.Flush();
requestStream.Close();


}
catch (Exception ex)
{
Console.WriteLine("..............Exception.......{0}", ex.Message);
}


}


gowthaman8870226416


Responses

Posted by: kgovindarao523-21772 on: 5/1/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Instead of sending each array element, just try to pass array elements as a comma separated string and just do the process in API method. In single API call, you can achieve your goal.


Thank you,
Govind

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 5/2/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Gowthaman,

You can use BackgroundWorker process.
Try these links:-
http://www.dotnetperls.com/backgroundworker
http://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners

Happy Coding.

Gow.Net, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response