http authenfication httpwebrequest C#内部500错误
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
我尝试过:
私人字符串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);
试试
{
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;
b $ b
}
}
catch(例外情况)
{
MessageBox.Show(ex.Message);
}
返回hidden_value;
}
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;
}
您确定webservice服务器已启动并正在运行吗?在我的情况下,服务器出现问题,我得到了相同的500错误。
要查看更多错误详情,请在IE中使用工具 - >互联网选项 - >高级 - >浏览 - >取消选中显示友好的HTTP错误消息。现在直接在浏览器中点击URL并检查错误消息。这可能会提供一些想法。
-Karthick Raju
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