c# - Bad Request 400 - protocol error on a valid url - Webclient -
i trying parse page (http://www.coleparmer.co.uk/product/turb_std_hach_2100q_kit/wz-99900-47) using webclient , having no luck.
var client = new webclient(); var html = client.downloadstring("http://www.coleparmer.co.uk/product/turb_std_hach_2100q_kit/wz-99900-47");
the appropriate headers needs set.
try { string html; using (webclient client = new webclient()) { client.headers.add("accept-language", " en-us"); client.headers.add("accept", " text/html, application/xhtml+xml, */*"); client.headers.add("user-agent", "mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; trident/5.0)"); html = client.downloadstring("http://www.coleparmer.co.uk/product/turb_std_hach_2100q_kit/wz-99900-47"); } } catch (webexception ex) { if (ex.status == webexceptionstatus.protocolerror && ex.response != null) { var resp = (httpwebresponse)ex.response; if (resp.statuscode == httpstatuscode.notfound) // http 404 { //handle } } //handle }
Comments
Post a Comment