c# - loop through list of URLs using HttpWebRequest causing error -



c# - loop through list of URLs using HttpWebRequest causing error -

looping through list of urls in mvc view

class="lang-html prettyprint-override">@foreach (var item in model) { httpwebrequest webrequest = httpwebrequest.create(@item.url) httpwebrequest; webrequest.method = webrequestmethods.http.get; webrequest.contenttype = "application/x-www-form-urlencoded"; using (httpwebresponse response = webrequest.getresponse() httpwebresponse) { if (response.statuscode == httpstatuscode.ok) { //do } else { //move next record } } }

i getting the fine when error occurs can not move next record.

i server error remote name not resolved: followed url in loop.

thanks, doug

always read documentation: getresponse method of httpwebrequest class can throw webexception, getting. basically, code never gets next record because it's throwing unhandled exception before that.

the lastly part of code should more like:

httpwebresponse response = null; seek { response = webrequest.getresponse() httpwebresponse; if (response.statuscode == httpstatuscode.ok) { //do } } grab (webexception ex) { } { if (response != null) response.dispose(); }

c# asp.net-mvc-4 httpwebrequest

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -