asp.net 模拟post的时候跳转过去的问题.
模拟post的时候能否自动跳转到post页面?.注意是在.CS 模拟post的情况
[解决办法]
/// <summary>
/// Post数据
/// </summary>
private static bool HttpPost(string sUrl, string data, ref string sError)
{
string postData = data;
try
{
if (string.IsNullOrEmpty("sUrl")) throw new Exception("URL地址有误");
//将数据提交到代理
WebRequest myWebRequest = WebRequest.Create(sUrl);
myWebRequest.Method = "POST";
myWebRequest.Timeout = 4000;
myWebRequest.ContentType = "application/x-www-form-urlencoded";
Stream streamReq = myWebRequest.GetRequestStream();
byte[] byteArray = Encoding.GetEncoding("GB2312").GetBytes(postData);
streamReq.Write(byteArray, 0, byteArray.Length);
streamReq.Close();
//获取系统返回的数据
WebResponse myWebResponse = myWebRequest.GetResponse();
StreamReader sr = new StreamReader(myWebResponse.GetResponseStream());
string res = sr.ReadToEnd();
sr.Close();
}
catch (Exception e)
{
}
}
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http:www.here.com/login.asp");
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
// Send the data.
newStream.Write(data,0,data.Length);
newStream.Close();
// Get response
HttpWebResponse myResponse=(HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(),Encoding.Default);
string content = reader.ReadToEnd();
Console.WriteLine(content);
</form>
<script type="text/javascript">
form1.submit();
</script>
</body>
前台用js提交form,form里的内容,后台由.cs产生。