开发者

HttpWebResponse return The remote server returned an error: (403) Forbidden

开发者 https://www.devze.com 2023-01-05 12:20 出处:网络
i want to get HTML output in http://www.belmondo.si/turisticna-ponudba/pocitnice/kratkirezultati?cid=ID&cityid=DPS&izhid=&trajanjeid=&oskrbaid=&kategorijaid=&ooseb=2&otrok

i want to get HTML output in

http://www.belmondo.si/turisticna-ponudba/pocitnice/kratkirezultati?cid=ID&cityid=DPS&izhid=&trajanjeid=&oskrbaid=&kategorijaid=&ooseb=2&otrok=0&lasten=1&prvic=1&rid=0-1&subtemplate=eksotika

but i always get

HTTPWEBRESPONSE The remote server returned an error: (403) Forbidden

I am using HttpWebResponse

protected string GetHtmlStringA(string url)
    {
        string sHtml = "";
        HttpWebRequest request;
        HttpWebResponse response = null;
        Stream stream = null;
        request = (HttpWebRequest)WebRequest.Create(url);
        response = (HttpWebResponse)request.GetResponse();
        stream = response.GetResponseStream();
        StreamReader sr = new StreamReader(stream, System.Text.Encoding.Default);
        sHtml = sr.ReadToEnd();
        if (stream != null) stream.Close();
        if (response != null) response.Close();
        return sHtml;
    }

i also try with UserAgent but it is t开发者_高级运维he same

req.request=
                    "Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.13) Gecko/2009073022 Firefox/3.0.13";

i can't find any solution on forums or internet


It seems you also need to send an Accept header. Sending a request with the following headers will work:

request.UserAgent = "Foo";
request.Accept = "*/*";


You need to pass authentication credentials with the web request:

request.Credentials = new NetworkCredentials("username", "password");


Make sure you have your credentials set correctly.

request.Credentials = CredentialCache.DefaultCredentials;

// if we have a proxy set its creds as well
if( request.Proxy != null )
{
   request.Proxy.Credentials = CredentialCache.DefaultCredentials;
}

If you need specific credentials you can create them this way

request.Credentials = new NetworkCredentials("username", "password");
0

精彩评论

暂无评论...
验证码 换一张
取 消