简体   繁体   中英

How to get HTTP response from website in C#?

I want to see if I enter URL + ' I will get a response, but I get an error: "403 forbidden"

here is my code:

foreach(string s in urls)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(s + "'");
    request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (request.GetResponse() == null)
        {
            Valid.Add(s);
            Console.WriteLine(s);
        }
    }
}

You need to set Credentials before making the call. The credentials should be prepared according to the web server authentication method.

1) if it is windows authentication, you can get the windows context credential by

System.Net.CredentialCache.DefaultNetworkCredentials
//if you know the user/pass, and domain
new NetworkCredential(sCredentialUserName, sCredentialPassword, sDomain);

this is usually the case in a enterprise domain environment.

2) if it is basic authentication and you know the user/pass, you can use same way to create the credential without domain.

new NetworkCredential(sCredentialUserName, sCredentialPassword);

About request and response, usually I prefer to use System.Net.WebClient and also create a wrapper class for it according to the requirements.

when you make the call, it would be something like this:

using (System.Net.WebClient cli = new System.Net.WebClient())
{
  ......
    //set credentials
    cli.Credentials = Credentials;
    //set headers
    cli.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    //make a sync call
    response = cli.UploadString(sRemoteUrl, "POST", sJsonRequest);
    ......
}

Hope it helps.

I can see that you are calling request.GetResponse() twice, why? Also you can get the 403 forbidden if there are some credentials needed or you are missing header. You may try to add to WebRequest the Method , ContentType (xml, json), Credentials (if there are any).,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM