1

I want to fill my MultiLine textbox from webpage's this is my code:

WebRequest request = WebRequest.Create(urltxt.Text.Trim());
WebResponse response = request.GetResponse();
Stream data = response.GetResponseStream();
string html = String.Empty;
using (StreamReader sr = new StreamReader(data))
{
    html = sr.ReadToEnd();
}

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");
valuetxt.Text = htmlBody.InnerText;

This code is working fine for some url but for some url (https) this gave me an error:

Could not find file 'C:\Program Files\IIS Express\www.justdial.com

or:

The remote server returned an error: (403) Forbidden

Can anyone help me? Thanks in advance, sorry for my bad English.

rodrigogq
  • 1,943
  • 1
  • 16
  • 25
Narender Godara
  • 105
  • 2
  • 10
  • In what of these lines are the exceptions comming from? – rodrigogq Feb 02 '18 at 14:58
  • WebResponse response = request.GetResponse(); – Narender Godara Feb 02 '18 at 14:59
  • Can you give examples of the Urls you're trying to hit. Also you're not telling `WebRequest` that it's a HTTPS conntection. Look at this Q&A: https://stackoverflow.com/questions/560804/how-do-i-use-webrequest-to-access-an-ssl-encrypted-site-using-https – jolySoft Feb 02 '18 at 15:44
  • I'm trying "https://www.justdial.com/Chandigarh/Packers-Movers/nct-10348289". Only www.justdial.com website give me an error. I tried some of online tools they also give me invalid url error. I think this is not my code problem. – Narender Godara Feb 02 '18 at 15:59

3 Answers3

0

It seems your address doesn't have http:// or https:// at the beginning; in the urltxt variable and you get error because of relative addressing.

Majid
  • 13,853
  • 15
  • 77
  • 113
  • Thanks. You solved my one problem "Could not find file 'C:\Program Files\IIS Express\...", But in https url i got "The remote server returned an error: (403) Forbidden" – Narender Godara Feb 02 '18 at 15:10
0

Are you behind a proxy? Even on open internet, depending on your network configuration, you might need to set credentials in your connection before requesting.

WebRequest request = WebRequest.Create(urltxt.Text.Trim());
request.Credentials = new NetworkCredential("user", "password");
rodrigogq
  • 1,943
  • 1
  • 16
  • 25
0

Add a UserAgent to your request to connect https properly:

request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";

from here

Majid
  • 13,853
  • 15
  • 77
  • 113