Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
I tried to sign in using httpwebrequest C# but I have faced internal 500 error..
Here is the raw data that I copied with fiddler
Please give me advice
Thank you 


Source is
   private string login_autocafe()
        {
            string hidden_value = Get_hidden_value();
            string url = "http://m.autocafe.co.kr/Home/CarmanagerLogin";
            string sendData = "__RequestVerificationToken="+ hidden_value + "&MemberName=%EA%B3%B5%EC%A3%BC%EC%8B%9D&password=1234";
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); 
                req.Method = "POST";
                req.Accept = "text/html, application/xhtml+xml, */*";
                req.Referer = "http://m.autocafe.co.kr/Home/CarmanagerLogin";
                req.Headers.Add("Accept-Language", "ko-KR");
                req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
                req.ContentType = "application/x-www-form-urlencoded";
                req.Headers.Add("Accept-Encoding", "gzip, deflate");
                req.Host = "m.autocafe.co.kr";
                req.Credentials = new NetworkCredential("%EA%B3%B5%EC%A3%BC%EC%8B%9D", "1234");
              
                req.KeepAlive = true;
                req.Headers.Add("Pragma", "no-cache");
                req.ServicePoint.Expect100Continue = false;

                req.CookieContainer = cookie; 
                req.PreAuthenticate = true;

                StreamWriter writer = new StreamWriter(req.GetRequestStream()); 
                writer.Write(sendData); 
                writer.Close();


                HttpWebResponse result = (HttpWebResponse)req.GetResponse();
                Stream respStream = result.GetResponseStream();
                string respStr = new StreamReader(respStream).ReadToEnd();
                cookie.Add(result.Cookies);

                
                if (result.StatusCode == HttpStatusCode.OK)
                {

                    return "Sucess";
                }

                else
                {
                    Console.WriteLine("Error");
                    return "ERR";
                }

            }

            catch (WebException webex)
            {
                var pageContent = new StreamReader(webex.Response.GetResponseStream()).ReadToEnd();
            
            //  setText(txt_comtiz_result, webex.Message);

            return "ERR";
                //MessageBox.Show(webex.Message);
            }


        }
        private string Get_hidden_value()
        {
            string hidden_value = "";

            WebClient client = new WebClient();
            string sourceUrl = client.DownloadString("http://m.autocafe.co.kr/Home/CarmanagerLogin");

            HtmlAgilityPack.HtmlDocument mydoc = new HtmlAgilityPack.HtmlDocument();

            mydoc.LoadHtml(sourceUrl);

            try
            {
                if (mydoc.DocumentNode != null)
                {

                    string test = mydoc.DocumentNode.SelectSingleNode("/html/body/div/input").OuterHtml;

                    hidden_value = mydoc.DocumentNode.SelectSingleNode("/html/body/div/input").Attributes["value"].Value;




                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return hidden_value;
        }


The data I got from fiddler is below

POST http://m.autocafe.co.kr/Home/CarmanagerLogin HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://m.autocafe.co.kr/Home/CarmanagerLogin
Accept-Language: ko-KR
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: m.autocafe.co.kr
Content-Length: 188
Connection: Keep-Alive
Pragma: no-cache
Cookie: __RequestVerificationToken=CjXgwUkuIWXIhjtph9k092MXAsQQLHtCbO-UIZihHUvJOvQXXKc8o4v5l7oClEefzz9k5VHSvJwAZmdJAAZatHL9Eq1a1h73oJrr4--vCSg1

__RequestVerificationToken=1x5V8SIxdLtR7r7znbdyYu0MF5tG5spK3DweQOVl3Jw2Zr0ADHAEqmZZog5qjcIC8tssLgfTgov6_tzBWUEMWl2UND9e6ZFsCmUHOmtLraE1&MemberName=%EA%B3%B5%EC%A3%BC%EC%8B%9D&password=1234


What I have tried:

private string Get_hidden_value()
{
string hidden_value = "";

WebClient client = new WebClient();
string sourceUrl = client.DownloadString("http://m.autocafe.co.kr/Home/CarmanagerLogin");

HtmlAgilityPack.HtmlDocument mydoc = new HtmlAgilityPack.HtmlDocument();

mydoc.LoadHtml(sourceUrl);

try
{
if (mydoc.DocumentNode != null)
{

string test = mydoc.DocumentNode.SelectSingleNode("/html/body/div/input").OuterHtml;

hidden_value = mydoc.DocumentNode.SelectSingleNode("/html/body/div/input").Attributes["value"].Value;




}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return hidden_value;
}
Posted
Updated 29-Jun-16 10:45am
v5
Comments
[no name] 29-Jun-16 7:26am    
You should start with this example : https://blogs.msdn.microsoft.com/ncl/2009/05/05/custom-http-authentication-schemes/

1 solution

Are you sure webservice server is up and running? In my case, there was a problem with server and I was getting the same 500 error.

To see more error details, In IE, Tools -> Internet Options -> Advanced -> Browsing -> Uncheck "show friendly HTTP error messages". Now hit the URL directly in browser and check the error message. This may give some idea.

-Karthick Raju
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900