Hello,
I am very new C#.
I am trying to log into http://mls.momls.com/SERetsMonmouth/Login.aspx with the following code and I am getting a 401 error. My user name and password are correct. I can manually log in through the browser.
But I am not sure about the code I wrote. It is based on what I read on the internet and after examining someone's code.
Please advice me on what I am doing wrong.

this.digestAuth = this.GetAuth();
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this.loginUrl);                     
            request.Accept = "*/*";
            request.UserAgent = this.userAgent;
            request.Headers.Add("RETS-Version", this.version);
            request.ProtocolVersion = HttpVersion.Version11;
            request.Headers.Add("Authorization", this.digestAuth);
            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri(this.loginUrl), "Digest", new NetworkCredential(this.userName, this.password));
            request.Credentials = cache;
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();

Recommended Answers

All 7 Replies

bump

Try using WebRequest instead of HttpWebRequest:

this.digestAuth = this.GetAuth();
            WebRequest request = WebRequest.Create(this.loginUrl);                     
            request.Accept = "*/*";
            request.UserAgent = this.userAgent;
            request.Headers.Add("RETS-Version", this.version);
            request.ProtocolVersion = HttpVersion.Version11;
            request.Headers.Add("Authorization", this.digestAuth);
            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri(this.loginUrl), "Digest", new NetworkCredential(this.userName, this.password));
            request.Credentials = cache;
            WebResponse response = request.GetResponse();

Thanks

>>Try using WebRequest instead of HttpWebRequest:

What does that have to do with the problem though? WebRequest is an abstract base class for Http requests, Ftp requests, etc. Even in your code its still creating an HttpWebRequest based on the URI format:

private void button3_Click(object sender, EventArgs e)
    {
      WebRequest req = WebRequest.Create(@"http://www.apexsoftware.com/");
      Console.WriteLine("Ojbect type: {0}", req.GetType());
      System.Diagnostics.Debugger.Break();
    }

Results in:

Ojbect type: System.Net.HttpWebRequest

ryy705: Use Wireshark to debug the login. First capture a login using a browser that works, then capture your login from code and see where the requests differ. Then you will be able to find out why the login is failing. A lot of sites use security tokens or some other means to obfuscate logging in to prevent spam automation.

Thank you for your response.

I tried to figure it out using wireshark and firebug.
At this point I am just trying to figure out what type of request is being sent when I submit the user name and password.(GET/POST)

Unfortunately, I can't figure it out (yet). It looks like .htaccess authentication. But the page is an aspx page.
Could you kindly tell me what this kind of authentication is called in Microsoft's vocabulary? This way I can google the name and find out how to log into it. If you click on http://mls.momls.com/SERetsMonmouth/Login.aspx . Many thanks in advance.

Its not Microsoft nominclature but rather industry terms ;)
Take a look around for "basic authentication".
http://www.google.com/search?hl=en&source=hp&q=C%23+HTTP+Basic+authentication&aq=f&oq=&aqi=g1
http://bytes.com/topic/c-sharp/answers/630284-asp-net-basic-authentication-programmatically
http://blog.kowalczyk.info/article/Forcing-basic-http-authentication-for-HttpWebReq.html

I believe you may need to support cookies as well. That site appears to be running on IIS and if it uses ASP.NET sessions then you will need to set a .CookieContainer on your HttpWebRequest.

Hi,

you need to add GetCredential to your code, The GetCredential(Uri, String) method searches the CredentialCache and returns the NetworkCredential instance for the specified URI and authorization type. If the CredentialCache contains no matching NetworkCredential instance, null is returned.

for Ref: http://msdn.microsoft.com/en-us/library/fy4394xd.aspx
--------------------------

Hello,
I am very new C#.
I am trying to log into http://mls.momls.com/SERetsMonmouth/Login.aspx with the following code and I am getting a 401 error. My user name and password are correct. I can manually log in through the browser.
But I am not sure about the code I wrote. It is based on what I read on the internet and after examining someone's code.
Please advice me on what I am doing wrong.

this.digestAuth = this.GetAuth();
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(this.loginUrl);                     
            request.Accept = "*/*";
            request.UserAgent = this.userAgent;
            request.Headers.Add("RETS-Version", this.version);
            request.ProtocolVersion = HttpVersion.Version11;
            request.Headers.Add("Authorization", this.digestAuth);
            CredentialCache cache = new CredentialCache();
            cache.Add(new Uri(this.loginUrl), "Digest", new NetworkCredential(this.userName, this.password));
            request.Credentials = cache;
            HttpWebResponse response = (HttpWebResponse) request.GetResponse();

Hi,

How we can add user agent password to login rets mls server with httpwerequest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.