本帖最后由 xionghui565 于 2018-5-6 15:13 编辑
源码中的网址都是单位nei网的服务器地址,登录和第一次cha询都没问题,但第二次cha询就报错“尝试自动重定向的次数太多”,请各位大神指点一下,谢谢!
(已在CSDN上发了帖,但无解决问题,原帖地址 https://bbs.csdn.net/topics/392357840)
(我用易语言 写了同样的功能(用的是"精易模块 "里的"网页_访问()"),可以实现多次cha询,所以我觉得还是c#里POST提交的时候,是不是还有什么信息没提交过去,而导致报错 )
c#源码
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_cha询_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";//nei网网址
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;
}
}
} 复制代码