c# - Trying to loop HttpWebRequest with no success -


i trying create program can sort number of results associated specified google search. need big table fast thought using loop. each time try though, debugger crashes due "system.windows.markup.xamlparseexception".

public long resultstat(string a)     {         var req = (httpwebrequest)webrequest.create("https://www.google.ca/search?hl=fr&output=search&sclient=psy-ab&q=a" + + "&btnk=");         using (req idisposable)         {             webresponse rep = req.getresponse();             stream str = rep.getresponsestream();             streamreader rdr = new streamreader(str);             string res = rdr.readtoend();             rdr.close();             //this code number results (it works perfectly)             int index = res.indexof(">environ");             int cond = 0;             string final = "";             try             {                 while (res[++index] != '<')                 {                     if (cond-- == 0 && res[index] != '&')                     { final += res[index]; cond = 0; }                     else if (res[index] == '&') cond = 5;                 }             }             catch { return 0; }             string temp = "";             foreach (char in final) if (i < 48 && > 58) temp += i;             return int64.parse(temp);         }     } 

this whole method used in main in loop such :

public void main() {     //other code     (int = 0; < 3; i++) resultstat(i.tostring()); // example     //other code } 

i know it's problem because comment loop, or lower 1 rep, nothing goes wrong. i've tried:

httpwebrequest().abort(); httpwebrequest().keepalive = false;

it didn't work

i don't think away doing correct way this. simple 1 can tell use lib curl c#. can send in array of urls , response array. perfect require here. here sample class code below multitasking itself. send in urls.

 public class multihttp {     public static string useragent = "mozilla 5.0";     public static string header = "content-type: application/x-www-form-urlencoded; charset=utf-8";     private static string[] result;      public static string[] multipost(string[] url, string post, int timeout)     {         result = new string[post.length];                    try         {             curl.globalinit((int)curlinitflag.curl_global_all);              easy.writefunction wf = new easy.writefunction(onwritedata);             //easy.headerfunction hf = new easy.headerfunction(onheaderdata);              easy[] easy = new easy[url.length];             multi multi = new multi();             (int = 0; < url.length; i++)             {                 if (url[i] != null)                 {                     easy[i] = new easy();                     easy[i].setopt(curloption.curlopt_url, url[i]);                     easy[i].setopt(curloption.curlopt_writefunction, wf);                     easy[i].setopt(curloption.curlopt_writedata, i);                     //easy[i].setopt(curloption.curlopt_headerfunction, hf);                     //easy[i].setopt(curloption.curlopt_headerdata, i);                     easy[i].setopt(curloption.curlopt_timeout, timeout);                     easy[i].setopt(curloption.curlopt_useragent, useragent);                     slist sl = new slist();                     sl.append(header);                     easy[i].setopt(curloption.curlopt_httpheader, sl);                     easy[i].setopt(curloption.curlopt_postfields, post);                     easy[i].setopt(curloption.curlopt_followlocation, true);                     easy[i].setopt(curloption.curlopt_post, true);                     //easy[i].setopt(curloption.curlopt_nobody, true);                      if (url[i].contains("https"))                     {                         easy[i].setopt(curloption.curlopt_ssl_verifyhost, 1);                         easy[i].setopt(curloption.curlopt_ssl_verifypeer, 0);                     }                     multi.addhandle(easy[i]);                 }             }              int stillrunning = 1;              while (multi.perform(ref stillrunning) == curlmcode.curlm_call_multi_perform) ;              while (stillrunning != 0)             {                 multi.fdset();                 int rc = multi.select(1000); // 1 second                 switch (rc)                 {                     case -1:                         stillrunning = 0;                         break;                      case 0:                     default:                         {                             while (multi.perform(ref stillrunning) == curlmcode.curlm_call_multi_perform) ;                             break;                         }                 }             }              // various cleanups             multi.cleanup();             (int = 0; < easy.length; i++)             {                 easy[i].cleanup();             }             curl.globalcleanup();         }         catch (exception)         {             //r = ex+"";         }         return result;     }      public static int32 onwritedata(byte[] buf, int32 size, int32 nmemb,         object extradata)     {         int tmp = convert.toint32(extradata.tostring()); ;         result[tmp] += system.text.encoding.utf8.getstring(buf);         return size * nmemb;     }        } 

call :

string[] url= new string[2]; url[1]="https://www.google.ca/search?hl=fr&output=search&sclient=psy-ab&q=a1&btnk="; url[2]="https://www.google.ca/search?hl=fr&output=search&sclient=psy-ab&q=a2&btnk=";  string poststring=""; // if not want post can same     thing keep url same send post array , change class works both ways string[] result = multihttp.multipost(url, poststring, timeout); 

its sample working idea sort out problem.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -