vb.net - .Net - WebClient or HttpWebRequest to get content -
i developed application download multiple images website pattern, using webclient. when started this, idea fast , easy app, made form with:
- webbrowser (wb)
- multiline textbox put multiple urls crawl
- listbox storage link of each photo
- textbox select targetpath
- button start process
it easy, needed find labels of each photos using + htmlelement + wb.document.all. once got links, used webclient download them fast (client.downloadfile(path, filename)). works flawlessly.
now app bigger, has more websites crawl , use backgroundworker manage this. started learn how bgworker works, , noticed cannot use control webbrowser, gives exception, cannot use wb.document.all links.
i found amazing example in codeproject of how download file bgworker, 1 concretely:
http://www.codeproject.com/articles/17979/downloading-files-in-net-with-all-information-prog
he uses httpwebrequest, seems harder implement if first need links.
my question is: should recode app use httpwebrequest or can adapt current code (webbrowser implicated) use bgworker? if it's possible use webbrowser bgworker, 1 correct way make it?
my current relevant code (i call function when click button). put necessary lines only:
function extractlicenseplate() dim client new webclient dim totalphotos integer = 0 dim finalplate string = "[no licenseplate found]" dim totalcarslist integer = txturls.lines.where(function(l) not string.isnullorwhitespace(l)).count() = 0 totalcarslist - 1 wb.navigate(txturls.lines(i)) waitforpageload() each ele htmlelement in wb.document.all 'ald if comboproviders.selectedindex = 0 if ele.getattribute("src").tolower.contains("iddoc") dim imgsrc string = ele.getattribute("src") lstphotos.items.add(imgsrc) end if 'get license plate make folders if ele.getattribute("action").tolower.contains("matr=") dim fullurlname string = ele.getattribute("action") finalplate = fullurlname.split("=")(3).substring(0, 7) end if end if next if (not directory.exists(txtdirectory.text & "\" & finalplate)) if not (finalplate = "[no licenseplate found]") directory.createdirectory(txtdirectory.text & "\" & finalplate) end if end if j = 0 lstphotos.items.count - 1 client.downloadfile(lstphotos.items(j).tostring, txtdirectory.text & "\" & finalplate & "\" & finalplate & j & ".jpg") client.dispose() next j lstphotos.items.clear() next return 0 end function
any appreciate.
thanks everyone.
i wouldn't use background worker, i'd write threads myself. here's example:
dim wc new webclient dim trdmain new thread(addressof fdownload) protected sub srun() trdmain.isbackground = true trdmain.start() end sub protected function fdownload() ' iterate through , download each str string in strimages wc.downloadstring(str, "your location") next return true ' terminate thread end function
this implies have array of urls of images wish download. implement code, simple , effective.
Comments
Post a Comment