在winform中用httpwebrequest表单post登录失败。

baolishicool 2009-12-13 04:04:18
在winform中用httpwebrequest表单post登录失败。报错:远程服务器返回错误: (500) 内部服务器错误。
为什么会这样呢?


-----------------------------------------以下是winform代码----------------------------------------


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using the_n = System.Net;
using the_en = System.Text;
using System.IO;
using System.Net;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{



try
{
the_n.HttpWebRequest hwr = (the_n.HttpWebRequest)the_n.HttpWebRequest.Create(@"http://www.nmec.org.cn/ViewScore/ViewScoreBS.aspx");


long lo = 4417831100984;

string the_data = @"__VIEWSTATE=/wEPDwUIMjcyNTAxMDZkZDsl2K15D71MYrXXjTh5uDX7N0Bk" +
@"&_EVENTVALIDATION=/wEWBQLZ76K7AwKu1/C5AgK047EHAqfj5cgLAvqc4c8EmQ1XrLWvzZ7O2XyE80W7uFla2Pg=" +
@"&txtXINGMING=aaa" +
@"&txtSFZH=440229192108042212" +
@"&txtZKZH=" + lo +
@"&__EVENTTARGET=" +
@"&__EVENTARGUMENT=";


Encoding encoding = Encoding.GetEncoding("GB2312");
hwr.AllowAutoRedirect = true;

byte[] sendda = encoding.GetBytes(the_data);
hwr.Method = "POST";
hwr.ContentType = "application/x-www-form-urlencoded";
hwr.ContentLength = sendda.Length;
System.IO.Stream newStream = hwr.GetRequestStream();
newStream.Write(sendda, 0, sendda.Length);
newStream.Close();


HttpWebResponse myResponse = (HttpWebResponse)hwr.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string content = reader.ReadToEnd();
this.textBox1.Text = content;

}
catch (Exception eee)
{


this.textBox1.Text = eee.Message.ToString();



}

}
}
}





--------------------------------------------------------以下是要求登录的HTML页面:----------------------------------------------------



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1"><title>
2009年医师资格考试医学综合笔试成绩查询
</title><link href="css/style.css" rel="Stylesheet" /></head>
<body>
<form name="form1" method="post" action="ViewScoreBS.aspx" onsubmit="javascript:return WebForm_OnSubmit();" id="form1">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUIMjcyNTAxMDZkZDsl2K15D71MYrXXjTh5uDX7N0Bk" />
</div>

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>


<script src="/ViewScore/WebResource.axd?d=312VsCNNFSWmWCQVxRgcyQ2&t=633595624183430636" type="text/javascript"></script>


<script src="/ViewScore/WebResource.axd?d=-OjobeIKMlqzP4Jcj_jozcaC6bmEFixugIcT6QnfsgA1&t=633595624183430636" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</script>

<div>

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWBQLZ76K7AwKu1/C5AgK047EHAqfj5cgLAvqc4c8EmQ1XrLWvzZ7O2XyE80W7uFla2Pg=" />
</div>
<table cellspacing="0" id="tblMain">
<tr>
<td valign="top" style="height: 100px;">
<img src="images/head1.gif" alt="" />
</td>
</tr>
<tr>
<td style="height: 7px; background: url(images/menu.gif);">
</td>
</tr>
<tr>
<td height="12" style="background: url(images/shadow.gif)">
</td>
</tr>
<tr>
<td align="center" style="background-color: #FFFFFF">
<br />
<table width="700" style="background-color: #F6F6F6">
<tr>
<td class="blacktitle">
<br />
2009年医师资格考试医学综合笔试成绩查询
<hr size="1" style="color: #C5C5C5" />

</td>
</tr>
<tr>
<td>
<table id="divShowBody" width="100%">
<tr>
<td class="label">
姓名:
</td>
<td class="content">
<input name="txtXINGMING" type="text" id="txtXINGMING" class="WenBen" />
</td>
<td align="left">
<span id="valrXINGMING" style="color:Red;display:none;"> 请填写姓名</span>
</td>
</tr>
<tr>
<td class="label">
身份证号:
</td>
<td class="content">
<input name="txtSFZH" type="text" id="txtSFZH" class="WenBen" />
</td>
<td align="left">
<span id="valrSFZH" style="color:Red;display:none;"> 请填写证件编号</span>
</td>
</tr>
<tr>
<td class="label">
准考证号:
</td>
<td class="content">
<input name="txtZKZH" type="text" id="txtZKZH" class="WenBen" />
</td>
<td align="left">
<span id="valrZKZH" style="color:Red;display:none;"> 请填写笔试准考证号</span>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<br />
<input type="submit" name="btnView" value="查  询" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("btnView", "", true, "", "", false, false))" id="btnView" />
</td>
</tr>
</table>

</td>
</tr>
<tr>
<td>

</td>
</tr>
</table>
<br />
<br />
<input type="submit" value="关闭窗口" onclick="window.close();return false;" />
<br />
<br />
</td>
</tr>
<tr>
<td height="10" style="background: url(images/footer.gif)">
</td>
</tr>
<tr>
<td>
<div id="foot1">
国家医学考试中心 版权所有  <a href="http://www.miibeian.gov.cn" target="_blank" title="中华人民共和国信息产业部ICP/IP地址信息备案号"
class="White">京ICP备07005584号</a>
</div>
</td>
</tr>
</table>

<script type="text/javascript">
//<![CDATA[
var Page_Validators = new Array(document.getElementById("valrXINGMING"), document.getElementById("valrSFZH"), document.getElementById("valrZKZH"));
//]]>
</script>

<script type="text/javascript">
//<![CDATA[
var valrXINGMING = document.all ? document.all["valrXINGMING"] : document.getElementById("valrXINGMING");
valrXINGMING.controltovalidate = "txtXINGMING";
valrXINGMING.focusOnError = "t";
valrXINGMING.errormessage = " 请填写姓名";
valrXINGMING.display = "Dynamic";
valrXINGMING.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
valrXINGMING.initialvalue = "";
var valrSFZH = document.all ? document.all["valrSFZH"] : document.getElementById("valrSFZH");
valrSFZH.controltovalidate = "txtSFZH";
valrSFZH.focusOnError = "t";
valrSFZH.errormessage = " 请填写证件编号";
valrSFZH.display = "Dynamic";
valrSFZH.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
valrSFZH.initialvalue = "";
var valrZKZH = document.all ? document.all["valrZKZH"] : document.getElementById("valrZKZH");
valrZKZH.controltovalidate = "txtZKZH";
valrZKZH.focusOnError = "t";
valrZKZH.errormessage = " 请填写笔试准考证号";
valrZKZH.display = "Dynamic";
valrZKZH.evaluationfunction = "RequiredFieldValidatorEvaluateIsValid";
valrZKZH.initialvalue = "";
//]]>
</script>


<script type="text/javascript">
//<![CDATA[

var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}

function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
//]]>
</script>
</form>
</body>
</html>
...全文
251 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
baolishicool 2009-12-13
  • 打赏
  • 举报
回复

[Quote=引用 1 楼 haiming15820 的回复:]
帮你顶住帖子
[/Quote]
==================
谢谢!
baolishicool 2009-12-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wuyq11 的回复:]
byte[] bs = Encoding.ASCII.GetBytes(param);

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create("");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;

是否存在
[/Quote]
===============================

存在。但是就是报错 。
wuyq11 2009-12-13
  • 打赏
  • 举报
回复
byte[] bs = Encoding.ASCII.GetBytes(param);

HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create("");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;

是否存在
haiming15820 2009-12-13
  • 打赏
  • 举报
回复
帮你顶住帖子

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧