POST登录成功后,第一次POST查询成功,第二次POST查询报错“尝试自动重定向的次数太多”,请各位大神指点一下

逍遥情天 2018-04-12 07:31:47
源码中的网址都是单位内网的服务器地址,登录和第一次查询都没问题,但第二次查询就报错“尝试自动重定向的次数太多”,请各位大神指点一下,谢谢!




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Security;
using System.Reflection; //反射
using System.IO;
using System.Net;


namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{

CookieContainer Cookie_登录 = new CookieContainer();

public Form1()
{
InitializeComponent();
}

private void button_刷新_Click(object sender, EventArgs e)
{
string url = "http://10.128.7.232/ssoserver/login";
string 返回的html = 网页POST(url, "", Cookie_登录, "utf-8");
string lt = 取中间文本(返回的html, "name=\"lt\" value=\"", "\" /");
string toSign = 取中间文本(返回的html, "name=\"toSign\" value=\"", "\" /");
label_lt.Text = lt;
label_toSign.Text = toSign;

string 验证码url = "http://10.128.7.232/ssoserver/CaptchaImg?j=" + JS_取随机数();
Image image = 网页POST图片(验证码url, "", Cookie_登录, "gb2312");
pictureBox_验证码.Image = image;
string str = Image_To_Base64(pictureBox_验证码.Image);
string 验证码 = 验证码识别(str);
textBox_验证码.Text = 验证码;
}

private void button_登录_Click(object sender, EventArgs e)
{
string 帐号 = "abc";
string 密码 = "123456";
string lt = label_lt.Text;
string toSign = label_toSign.Text;
string 验证码 = textBox_验证码.Text;
string 返回的html = "";
string url = "http://10.128.7.232/ssoserver/login";
string post = "signedData=&toSign=" + toSign + "&username=" + 帐号 + "&password=" + 密码 + "&vcode=" + 验证码 + "<=" + lt + "&_eventId=submit";

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);//新建一个HttpWebRequest
myHttpWebRequest.Headers.Add("Accept-Language", "zh-cn");
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = post.Length;
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; zh-CN; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8";
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = Cookie_登录;

Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));//"gb2312","utf-8"
myStreamWriter.Write(post);
myStreamWriter.Close();
myRequestStream.Close();

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();//新建一个HttpWebResponse
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));//"gb2312","utf-8"
返回的html = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

if (返回的html.IndexOf("工作台") > 0)
MessageBox.Show("登录成功");
else
MessageBox.Show("登录失败");
}

private void button_查询_Click(object sender, EventArgs e)
{
string 身份证 = "420124197905260031";
string url = "http://10.128.7.231:8080/hbcj/complex/employeeSimpleQueryAction!getEmployeeInfoRPC.do";
string post = "dto.jstj=" + 身份证 + "&___businessId=531956";
string 返回的html = "";
返回的html = 网页POST(url, post, Cookie_登录, "utf-8");
string 基本信息 = 取中间文本(返回的html, "),new Array('", "'))").Replace("','", ",");
if (基本信息 == "")
{
MessageBox.Show("没有找到此人的信息");
}
else
{
MessageBox.Show("查找此人信息成功");
textBox_返回的html.Text = 返回的html;
}

}

private string 网页POST(string url, string post, CookieContainer cookie, string 编码)
{
string 返回的html = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.Headers.Add("Accept-Language", "zh-cn");
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.UserAgent = "Mozilla/5.0 (compatible;Windows NT 6.1; WOW64;Trident/6.0;MSIE 9.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.27 Safari/537.36";
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = cookie;

Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
myStreamWriter.Write(post);
myStreamWriter.Close();
myRequestStream.Close();

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream myResponseStream = myHttpWebResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(编码));
返回的html = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return 返回的html;
}

private Image 网页POST图片(string url, string post, CookieContainer cookie, string 编码)
{
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.ContentLength = post.Length;
myHttpWebRequest.Method = "POST";
myHttpWebRequest.CookieContainer = cookie;

Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));//"gb2312","utf-8"
myStreamWriter.Write(post);
myStreamWriter.Close();
myRequestStream.Close();

HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream imgStream = myHttpWebResponse.GetResponseStream();
Image image = Image.FromStream(imgStream);
imgStream.Close();
return image;
}





public string JS_取随机数()
{
Type obj = Type.GetTypeFromProgID("ScriptControl");
if (obj == null) return null;
object ScriptControl = Activator.CreateInstance(obj);
obj.InvokeMember("Language", BindingFlags.SetProperty, null, ScriptControl, new object[] { "JavaScript" });
string js = "function refeshCode(){ return Math.random(); }";
obj.InvokeMember("AddCode", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { js });
return obj.InvokeMember("Eval", BindingFlags.InvokeMethod, null, ScriptControl, new object[] { "refeshCode()" }).ToString();
}

public string 验证码识别(string base64string)
{
CookieContainer Cookie_空 = new CookieContainer();//没有用,只是为了不让"网页POST()"缺少参数.
string url = "http://10.129.136.129:3600/ajax.php";//内网网址
string post = STR_TO_UTF8("请求") + "=" + STR_TO_UTF8("通用文字识别") + "&base64_img=" + base64string;
string 返回的html = 网页POST(url, post, Cookie_空, "gb2312");
string[] 结果集 = 返回的html.Split('^');
if (结果集[0] != "通用文字识别成功")
{
MessageBox.Show(结果集[1]);
return "";
}

if (结果集.Length != 2)
{
MessageBox.Show(结果集[1]);
return "";
}

return 结果集[1];
}

private string STR_TO_UTF8(string txt)
{
string code = "";
foreach (byte b in Encoding.UTF8.GetBytes(txt))
{
code += '%' + b.ToString("X");
}
return code;
}

private string 取中间文本(string 全文本, string 前面文本, string 后面文本)
{
int 位置A = 全文本.IndexOf(前面文本);
int 位置B = 全文本.IndexOf(后面文本, 位置A + 1);
if (位置A < 0 || 位置B < 0)
{
return "";
}
else
{
位置A = 位置A + 前面文本.Length;
位置B = 位置B - 位置A;
if (位置A < 0 || 位置B < 0)
{
return "";
}
return 全文本.Substring(位置A, 位置B);
}
}

public string Image_To_Base64(Image image)
{
string strbaser64 = "";
try
{
Bitmap bmp = new Bitmap(image);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
strbaser64 = Convert.ToBase64String(arr);
}
catch (Exception)
{
throw new Exception("Something wrong during convert!");
}
return strbaser64;
}
}
}
...全文
1554 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
逍遥情天 2018-04-20
  • 打赏
  • 举报
回复
搞不定啊,请精通POST的大神进来指点一下
liaokm168 2018-04-17
  • 打赏
  • 举报
回复
引用 5 楼 xionghui565 的回复:
liaokm168: 按你说的关闭重定向,第二次查询就没有报错了,但返回的也不是要查询的结果,返回信息如下:

<html><head><title>302 Moved Temporarily</title></head>
<body bgcolor="#FFFFFF">
<p>This document you requested has moved 
temporarily.</p>
<p>It's now at <a href="http://10.128.7.232:80/ssoserver/login?service=http%3A%2F%2F10.128.7.231%3A8080%2Fhbcj%2Fj_spring_cas_security_check%3F&stService=ZujbJg53OJz9prDtrWxo6dGo8I8vVGZIVJDxjs091dU%3D">http://10.128.7.232:80/ssoserver/login?service=http%3A%2F%2F10.128.7.231%3A8080%2Fhbcj%2Fj_spring_cas_security_check%3F&stService=ZujbJg53OJz9prDtrWxo6dGo8I8vVGZIVJDxjs091dU%3D</a>.</p>
</body></html>
我当时也是给302搞的头疼..后来其实不是代码问题,而且不要去调试..中间到底是什么原因导致的.我也不太清楚
逍遥情天 2018-04-16
  • 打赏
  • 举报
回复
应该不是服务器端业务逻辑控制问题,因为我用易语言写了同样的功能(用的是"精易模块"里的"网页_访问()"),可以实现多次查询,所以我觉得还是c#里POST提交的时候,是不是还有什么信息不提交过去,而导致报错
逍遥情天 2018-04-16
  • 打赏
  • 举报
回复
liaokm168: 按你说的关闭重定向,第二次查询就没有报错了,但返回的也不是要查询的结果,返回信息如下:

<html><head><title>302 Moved Temporarily</title></head>
<body bgcolor="#FFFFFF">
<p>This document you requested has moved 
temporarily.</p>
<p>It's now at <a href="http://10.128.7.232:80/ssoserver/login?service=http%3A%2F%2F10.128.7.231%3A8080%2Fhbcj%2Fj_spring_cas_security_check%3F&stService=ZujbJg53OJz9prDtrWxo6dGo8I8vVGZIVJDxjs091dU%3D">http://10.128.7.232:80/ssoserver/login?service=http%3A%2F%2F10.128.7.231%3A8080%2Fhbcj%2Fj_spring_cas_security_check%3F&stService=ZujbJg53OJz9prDtrWxo6dGo8I8vVGZIVJDxjs091dU%3D</a>.</p>
</body></html>
liaokm168 2018-04-16
  • 打赏
  • 举报
回复
request.AllowAutoRedirect = false; 关闭重定向
逍遥情天 2018-04-13
  • 打赏
  • 举报
回复
请各位大神指点一下,谢谢!
  • 打赏
  • 举报
回复
服务器端调试、测试。如果服务器是别人的,那就请别人的技术支持部门回答你的问题。 这基本上就是服务器端业务逻辑控制问题。比如说你的客户端程序没有使用登录信息来提供后续操作数据,那么服务器端就把你“扔出去了”,就会产生服务失败问题。

110,538

社区成员

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

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

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