Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
i have a written a piece of code which sends and receives request.

string val = objclient.GetAsync(struri).Result.RequestMessage.RequestUri.AbsoluteUri.ToString();

but when i run this i a loop for about 15 uri's it takes a lot of time.


for(int i =0;i<= 16;i++)
{
string val = objclient.GetAsync(uri[i]).Result.RequestMessage.RequestUri.AbsoluteUri.ToString();
}

Is there any way to optimize this piece of code.
Posted

1 solution

Why through HttpClient? It's much easier to do via the class System.Net.HttpWebRequest:
http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx[^].

The technique of getting a HTTP response is shown on the MSDN help page of its ancestor class System.Net.HttpRequest:
http://msdn.microsoft.com/en-us/library/system.net.webrequest.aspx[^].

The method is blocking and synchronous, which is much better I think. You can explicitly execute it in a separate thread, is needed; and this form of asynchronous behavior is much better, because you reuse the same methods of threading and thread synchronization again and again.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900