c# - I am downloading a link from nseindia site using my program but now I cant do it there is a error 403? -
i downloading file nseindia site using program there error 403(forbidden)(page not found). value same site using code
webclient client = new webclient(); client.headers.add("user-agent", "mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; wow64; trident/5.0)"); stream data; try { data = client.openread("http://www.nseindia.com/"); } catch (exception e) { messagebox.show("error: " + e.message + e.data + e.helplink); return ""; } streamreader reader = new streamreader(data); string s = null; int count = 0; while (reader.read()>0) { s = reader.readline(); if (s.contains("<td class=\"t1\">")) { messagebox.show("line: " + s); s= s.remove(0, 18); s = s.remove(s.length - 5); count++; if (count == 5) break; } } data.close(); reader.close(); return s;
it seems site requires accept
http request header:
client.headers[httprequestheader.accept] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
one of problems encounter doing totally dependent on how site trying scrape works. not mention fragility of html parsing code. worse change anytime , have no control unless own site. tomorrow site might start requiring other http header , code stop working once again. saying prepared.
maybe contact site owners , see if offering official api consume content.
Comments
Post a Comment