Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
AnswerRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
arnold_w17-May-20 23:37
arnold_w17-May-20 23:37 
GeneralRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
Richard MacCutchan18-May-20 0:51
mveRichard MacCutchan18-May-20 0:51 
GeneralRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
arnold_w18-May-20 1:32
arnold_w18-May-20 1:32 
AnswerRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
arnold_w17-May-20 23:44
arnold_w17-May-20 23:44 
AnswerRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
Richard Deeming18-May-20 0:52
mveRichard Deeming18-May-20 0:52 
GeneralRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
arnold_w18-May-20 1:26
arnold_w18-May-20 1:26 
GeneralRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
Richard Deeming18-May-20 1:35
mveRichard Deeming18-May-20 1:35 
GeneralRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
arnold_w18-May-20 1:58
arnold_w18-May-20 1:58 
Using the example code posted on the web page you linked to in your post, I was able to modify my code to the following:
C#
public static GenericStatusType LoadHttpPageWithBasicAuthentication(string url, string username, string password)
{
    GenericStatusType status = new GenericStatusType(GenericStatusCode.OK, null, "", "");
    try
    {
        Uri myUri = new Uri(url);
        HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
        myHttpWebRequest.UserAgent = ".NET Framework Test Client";
        NetworkCredential myNetworkCredential = new NetworkCredential(username, password);
        CredentialCache myCredentialCache = new CredentialCache();
        myCredentialCache.Add(myUri, "Basic", myNetworkCredential);
        myHttpWebRequest.PreAuthenticate = true;
        myHttpWebRequest.Credentials = myCredentialCache;
        HttpWebResponse myWebResponse = null;
        try
        {
            myWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        }
        catch (WebException e)
        {
            status.errorHeading = "Error Reading Web Page";
            status.errorMsg = "A WebException occurred while trying to read from the following URL: " + url + ". Technical information: " + e.Message;
            status.statusCode = GenericStatusCode.ERROR;
            if (myWebResponse != null)
            {
                myWebResponse.Close();
            }
            return status;
        }

        Stream responseStream = myWebResponse.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default);
        string pageContent = myStreamReader.ReadToEnd();
        responseStream.Close();
        myWebResponse.Close();
        status.value = pageContent;
    }
    catch (Exception e)
    {
        status.errorHeading = "Error Reading Web Page";
        status.errorMsg = "An error occurred while trying to read from the following URL: " + url + ". Technical information: " + e.Message;
        status.statusCode = GenericStatusCode.ERROR;
    }
    return status;
}
As far as I can tell, this works great! For some reason, some values get changed (for example, ;_hide=;title;" id="x23866"> becomes ;_hide=;title;" id="x1295">), but that doesn't matter in my use case. Thanks for solving this!
GeneralRe: Download a webpage from within C# in exactly the same way a web browser would? Pin
Luc Pattyn18-May-20 1:41
sitebuilderLuc Pattyn18-May-20 1:41 
QuestionQoL Debugging Pin
Super Lloyd17-May-20 14:34
Super Lloyd17-May-20 14:34 
QuestionHow to get the form at design time to the center of the screen Pin
Member 1297423515-May-20 17:15
Member 1297423515-May-20 17:15 
AnswerRe: How to get the form at design time to the center of the screen Pin
Richard MacCutchan15-May-20 22:03
mveRichard MacCutchan15-May-20 22:03 
AnswerRe: How to get the form at design time to the center of the screen Pin
kalberts16-May-20 4:13
kalberts16-May-20 4:13 
AnswerRe: How to get the form at design time to the center of the screen Pin
Dave Kreskowiak16-May-20 4:38
mveDave Kreskowiak16-May-20 4:38 
GeneralRe: How to get the form at design time to the center of the screen Pin
Member 1297423516-May-20 9:27
Member 1297423516-May-20 9:27 
GeneralRe: How to get the form at design time to the center of the screen Pin
Dave Kreskowiak16-May-20 9:42
mveDave Kreskowiak16-May-20 9:42 
GeneralRe: How to get the form at design time to the center of the screen Pin
kalberts16-May-20 10:57
kalberts16-May-20 10:57 
QuestionReflection for Class Pin
Oscar K.14-May-20 0:57
Oscar K.14-May-20 0:57 
AnswerRe: Reflection for Class Pin
Pete O'Hanlon14-May-20 1:33
mvePete O'Hanlon14-May-20 1:33 
GeneralRe: Reflection for Class Pin
Oscar K.14-May-20 2:01
Oscar K.14-May-20 2:01 
GeneralRe: Reflection for Class Pin
Eddy Vluggen14-May-20 2:43
professionalEddy Vluggen14-May-20 2:43 
GeneralRe: Reflection for Class Pin
Richard Deeming14-May-20 2:46
mveRichard Deeming14-May-20 2:46 
GeneralRe: Reflection for Class Pin
Oscar K.14-May-20 4:33
Oscar K.14-May-20 4:33 
QuestionIs there a way to implement radio button in property grid Pin
Member 1482116713-May-20 1:33
Member 1482116713-May-20 1:33 
GeneralRe: Is there a way to implement radio button in property grid Pin
harold aptroot13-May-20 2:14
harold aptroot13-May-20 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.