110,538
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 去图形边框
/// </summary>
/// <param name="borderWidth"></param>
public void ClearPicBorder(int borderWidth)
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
if (i < borderWidth || j < borderWidth || j > bmpobj.Width - 1 - borderWidth || i > bmpobj.Height - 1 - borderWidth)
bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
}
}
}
/// <summary>
/// 灰度转换,逐行方式
/// </summary>
public void GrayByLine()
{
Rectangle rec = new Rectangle(0, 0, bmpobj.Width, bmpobj.Height);
BitmapData bmpData = bmpobj.LockBits(rec, ImageLockMode.ReadWrite, bmpobj.PixelFormat);// PixelFormat.Format32bppPArgb);
// bmpData.PixelFormat = PixelFormat.Format24bppRgb;
IntPtr scan0 = bmpData.Scan0;
int len = bmpobj.Width * bmpobj.Height;
int[] pixels = new int[len];
Marshal.Copy(scan0, pixels, 0, len);
//对图片进行处理
int GrayValue = 0;
for (int i = 0; i < len; i++)
{
GrayValue = GetGrayNumColor(Color.FromArgb(pixels[i]));
pixels[i] = (byte)(Color.FromArgb(GrayValue, GrayValue, GrayValue)).ToArgb(); //Color转byte
}
bmpobj.UnlockBits(bmpData);
////输出
//GCHandle gch = GCHandle.Alloc(pixels, GCHandleType.Pinned);
//bmpOutput = new Bitmap(bmpobj.Width, bmpobj.Height, bmpData.Stride, bmpData.PixelFormat, gch.AddrOfPinnedObject());
//gch.Free();
}
/// <summary>
/// 得到有效图形并调整为可平均分割的大小
/// </summary>
/// <param name="dgGrayValue">灰度背景分界值</param>
/// <param name="CharsCount">有效字符数</param>
/// <returns></returns>
public void GetPicValidByValue(int dgGrayValue, int CharsCount)
{
int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
int posx2 = 0; int posy2 = 0;
for (int i = 0; i < bmpobj.Height; i++) //找有效区
{
for (int j = 0; j < bmpobj.Width; j++)
{
int pixelValue = bmpobj.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根据灰度值
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
// 确保能整除
int Span = CharsCount - (posx2 - posx1 + 1) % CharsCount; //可整除的差额数
if (Span < CharsCount)
{
int leftSpan = Span / 2; //分配到左边的空列 ,如span为单数,则右边比左边大1
if (posx1 > leftSpan)
posx1 = posx1 - leftSpan;
if (posx2 + Span - leftSpan < bmpobj.Width)
posx2 = posx2 + Span - leftSpan;
}
//复制新图
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
}
/// <summary>
/// 得到有效图形,图形为类变量
/// </summary>
/// <param name="dgGrayValue">灰度背景分界值</param>
/// <param name="CharsCount">有效字符数</param>
/// <returns></returns>
public void GetPicValidByValue(int dgGrayValue)
{
int posx1 = bmpobj.Width; int posy1 = bmpobj.Height;
int posx2 = 0; int posy2 = 0;
for (int i = 0; i < bmpobj.Height; i++) //找有效区
{
for (int j = 0; j < bmpobj.Width; j++)
{
int pixelValue = bmpobj.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根据灰度值
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
//复制新图
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
bmpobj = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);
}
/// <summary>
/// 得到有效图形,图形由外面传入
/// </summary>
/// <param name="dgGrayValue">灰度背景分界值</param>
/// <param name="CharsCount">有效字符数</param>
/// <returns></returns>
public Bitmap GetPicValidByValue(Bitmap singlepic, int dgGrayValue)
{
int posx1 = singlepic.Width; int posy1 = singlepic.Height;
int posx2 = 0; int posy2 = 0;
for (int i = 0; i < singlepic.Height; i++) //找有效区
{
for (int j = 0; j < singlepic.Width; j++)
{
int pixelValue = singlepic.GetPixel(j, i).R;
if (pixelValue < dgGrayValue) //根据灰度值
{
if (posx1 > j) posx1 = j;
if (posy1 > i) posy1 = i;
if (posx2 < j) posx2 = j;
if (posy2 < i) posy2 = i;
};
};
};
//复制新图
Rectangle cloneRect = new Rectangle(posx1, posy1, posx2 - posx1 + 1, posy2 - posy1 + 1);
return singlepic.Clone(cloneRect, singlepic.PixelFormat);
}
/// <summary>
/// 平均分割图片
/// </summary>
/// <param name="RowNum">水平上分割数</param>
/// <param name="ColNum">垂直上分割数</param>
/// <returns>分割好的图片数组</returns>
public Bitmap[] GetSplitPics(int RowNum, int ColNum)
{
if (RowNum == 0 || ColNum == 0)
return null;
int singW = bmpobj.Width / RowNum;
int singH = bmpobj.Height / ColNum;
Bitmap[] PicArray = new Bitmap[RowNum * ColNum];
Rectangle cloneRect;
for (int i = 0; i < ColNum; i++) //找有效区
{
for (int j = 0; j < RowNum; j++)
{
cloneRect = new Rectangle(j * singW, i * singH, singW, singH);
PicArray[i * RowNum + j] = bmpobj.Clone(cloneRect, bmpobj.PixelFormat);//复制小块图
}
}
return PicArray;
}
/// <summary>
/// 返回灰度图片的点阵描述字串,1表示灰点,0表示背景
/// </summary>
/// <param name="singlepic">灰度图</param>
/// <param name="dgGrayValue">背前景灰色界限</param>
/// <returns></returns>
public string GetSingleBmpCode(Bitmap singlepic, int dgGrayValue)
{
Color piexl;
string code = "";
for (int posy = 0; posy < singlepic.Height; posy++)
for (int posx = 0; posx < singlepic.Width; posx++)
{
piexl = singlepic.GetPixel(posx, posy);
if (piexl.R < dgGrayValue) // Color.Black )
code = code + "1";
else
code = code + "0";
}
return code;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WindowsApplication1
{
class UnCodebase
{
public Bitmap bmpobj;
public UnCodebase(Bitmap pic)
{
// if (pic.PixelFormat == PixelFormat.Format8bppIndexed)
bmpobj = new Bitmap(pic); //转换为Format32bppRgb
}
/// <summary>
/// 根据RGB,计算灰度值
/// </summary>
/// <param name="posClr">Color值</param>
/// <returns>灰度值,整型</returns>
private int GetGrayNumColor(System.Drawing.Color posClr)
{
return (posClr.R * 19595 + posClr.G * 38469 + posClr.B * 7472) >> 16;
}
/// <summary>
/// 灰度转换,逐点方式
/// </summary>
public void GrayByPixels()
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
int tmpValue = GetGrayNumColor(bmpobj.GetPixel(j, i));
bmpobj.SetPixel(j, i, Color.FromArgb(tmpValue, tmpValue, tmpValue));
}
}
}
/// <summary>
/// 去除低于此值的点 加强高于此值的点
/// </summary>
/// <param name="yanse"> 对比 值</param>
public void GrayByPixels2(int yanse)
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
Color c = bmpobj.GetPixel(j, i);
if (c.R >= yanse)
bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
else
bmpobj.SetPixel(j, i, Color.FromArgb(0, 0, 0));
}
}
}
public void GrayByPixels2_fan(int yanse)
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
Color c = bmpobj.GetPixel(j, i); //获取图片RGB值
if (c.R >= yanse)
bmpobj.SetPixel(j, i, Color.FromArgb(0, 0, 0));
else
bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
}
}
}
/// <summary>
/// 保留蓝色
/// </summary>
/// <param name="yanse"> 对比 值</param>
public void baoliu_blue(int yanse)
{
for (int i = 0; i < bmpobj.Height; i++)
{
for (int j = 0; j < bmpobj.Width; j++)
{
Color c = bmpobj.GetPixel(j, i);
if (c.B <= yanse && c.B >= 0)
bmpobj.SetPixel(j, i, Color.FromArgb(255, 255, 255));
//else
// bmpobj.SetPixel(j, i, Color.FromArgb(0, 0, 0));
}
}
}
/// <summary>
/// 去掉杂点(适合杂点/杂线粗为1)
/// </summary>
/// <param name="dgGrayValue">背前景灰色界限</param>
/// <returns></returns>
public void ClearNoise(int dgGrayValue, int MaxNearPoints, int juli)
{
Color piexl;
int nearDots = 0;
int XSpan, YSpan, tmpX, tmpY;
//逐点判断
for (int i = 0; i < bmpobj.Width; i++)
for (int j = 0; j < bmpobj.Height; j++)
{
try
{
piexl = bmpobj.GetPixel(i, j);
if (piexl.R < dgGrayValue)
{
nearDots = 0;
//判断周围8个点是否全为空
if (i < 0 + juli || i > bmpobj.Width - juli || j < 0 + juli || j > bmpobj.Height - 1) //边框全去掉
{
//bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
else
{
if (bmpobj.GetPixel(i - juli, j - juli).R == dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i, j - juli).R == dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + juli, j - juli).R == dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i - juli, j).R == dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + juli, j).R == dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i - juli, j + juli).R == dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i, j + juli).R == dgGrayValue) nearDots++;
if (bmpobj.GetPixel(i + juli, j + juli).R == dgGrayValue) nearDots++;
}
if (nearDots >= MaxNearPoints)
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255)); //去掉单点 && 粗细小3邻边点
}
else //背景
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
catch (Exception err)
{
}
}
}
/// <summary>
/// 去掉杂点(适合杂点/杂线粗为1)
/// </summary>
/// <param name="dgGrayValue">背前景灰色界限</param>
/// <returns></returns>
public void ClearNoise2(int dgGrayValue, int juli)
{
Color piexl;
int nearDots = 0;
int XSpan, YSpan, tmpX, tmpY;
//逐点判断
for (int i = 0; i < bmpobj.Width; i++)
for (int j = 0; j < bmpobj.Height; j++)
{
try
{
if (i <= 0 + juli || i >= bmpobj.Width - juli || j <= 0 + juli || j >= bmpobj.Height - juli) //边框全去掉
{
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
piexl = bmpobj.GetPixel(i, j);
if (piexl.R < dgGrayValue)
{
piexl = bmpobj.GetPixel(i, j);
int W = 0; //横向标志
int H = 0; //纵向标志
nearDots = 0;
//判断周围8个点是否全为空
if (i < 0 + juli || i > bmpobj.Width - juli || j < 0 + juli || j > bmpobj.Height - juli) //边框全去掉
{
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 255));
}
else
{
int a1 = bmpobj.GetPixel(i, j - juli).R;
int a2 = bmpobj.GetPixel(i, j + juli).R;
int b1 = bmpobj.GetPixel(i - juli, j).R;
int b2 = bmpobj.GetPixel(i + juli, j).R;
//上下测距
if (a1 > dgGrayValue && a2 > dgGrayValue)
{
H = 1;
}
//左右侧距
if (b1 > dgGrayValue && b2 > dgGrayValue)
{
W = 1;
}
if (H == 1 || W == 1)
{
bmpobj.SetPixel(i, j, Color.FromArgb(255, 255, 30));
}
}
}
else
{
//bmpobj.SetPixel(i, j, Color.FromArgb(0, 0, 0));
}
}
catch (Exception err)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace WindowsApplication1
{
class unCodeAiYing : UnCodebase
{
//字符表 顺序为0..9,A..Z,a..z
string[] CodeArray = new string[] {
"011110100001100001101101101101101101101101100001100001011110",
"00100111000010000100001000010000100001000010011111",
"011110100001100001000001000010000100001000010000100001111111",
"011110100001100001000010001100000010000001100001100001011110",
"000100000100001100010100100100100100111111000100000100001111",
"111111100000100000101110110001000001000001100001100001011110",
"001110010001100000100000101110110001100001100001100001011110",
"111111100010100010000100000100001000001000001000001000001000",
"011110100001100001100001011110010010100001100001100001011110",
"011100100010100001100001100011011101000001000001100010011100",
//A-Z
"0001000000100000101000010100001010000101000111110010001001000101110111",
"1111110010000101000010100010011110001000100100001010000101000011111110",
"0011111010000110000011000000100000010000001000000100000101000100011100",
"1111100010001001000010100001010000101000010100001010000101000101111100",
"1111110010000101001000100100011110001001000100100010000001000011111110",
"1111110010000101001000100100011110001001000100100010000001000001110000",
"0011110010001010000101000000100000010000001000111100001001000100011100",
"1110111010001001000100100010011111001000100100010010001001000101110111",
"11111001000010000100001000010000100001000010011111",
"0011111000010000001000000100000010000001000000100000010010001001111000",
"1110111010001001001000101000011100001010000100100010010001000101110111",
"1110000010000001000000100000010000001000000100000010000001000011111111",
"1110111011011001101100110110010101001010100101010010101001010101101011",
"1110111011001001100100101010010101001010100100110010011001001101110010",
"0011100010001010000011000001100000110000011000001100000101000100011100",
"1111110010000101000010100001011111001000000100000010000001000001110000",
"0011100010001010000011000001100000110000011000001101100101001100011101",
"1111100010001001000100100010011110001010000100100010010001000101110011",
"011111100001100001100000011000000110000001100001100001111110",
"1111111100100100010000001000000100000010000001000000100000010000011100",
"1110111010001001000100100010010001001000100100010010001001000100011100",
"1110111010001001000100100010001010000101000010100001010000010000001000",
"1101011010101001010100101010010101001101100010100001010000101000010100",
"1110111010001000101000010100000100000010000010100001010001000101110111",
"1110111010001001000100010100001010000010000001000000100000010000011100",
"111111100010000010000100000100001000001000010000010001111111",
//a-z
};
public unCodeAiYing(Bitmap pic)
: base(pic)
{
}
public string getPicnum()
{
GrayByPixels(); //灰度处理
//GrayByPixels2(); //灰度杂点处理
ClearNoise(255, 8,1);//纯度杂点处理
// GetPicValidByValue(128, 4); //得到有效空间
GetPicValidByValue(128, 4); //得到有效空间
Bitmap[] pics = GetSplitPics(4, 1); //分割
if (pics.Length != 4)
{
return ""; //分割错误
}
else // 重新调整大小
{
pics[0] = GetPicValidByValue(pics[0], 128);
pics[1] = GetPicValidByValue(pics[1], 128);
pics[2] = GetPicValidByValue(pics[2], 128);
pics[3] = GetPicValidByValue(pics[3], 128);
}
// if (!textBoxInput.Text.Equals(""))
string result = "";
char singleChar = ' ';
{
for (int i = 0; i < 4; i++)
{
string code = GetSingleBmpCode(pics[i], 128); //得到代码串
float xiangsi_old = 0;
float xiangsi_old_per=0;
int arrayIndex_temp = 0;
for (int arrayIndex = 0; arrayIndex < CodeArray.Length; arrayIndex++)
{
string stringtemp = CodeArray[arrayIndex];
float xiangsi = 0;
float xiangsitrue = 0;
//if (Math.Abs(code.Length - CodeArray[arrayIndex].Length) <= 5)
if (code.Length >= CodeArray[arrayIndex].Length || CodeArray[arrayIndex].Length - code.Length>=1)
{
for (int codet = 0; codet < Math.Min(CodeArray[arrayIndex].Length, code.Length); codet++)
{
if (code.Substring(codet, 1) == CodeArray[arrayIndex].Substring(codet, 1))
{
xiangsi += 1;
xiangsitrue += 1;
}
if (code.Substring(codet, 1) != CodeArray[arrayIndex].Substring(codet, 1))
{
xiangsi -= 1;
}
}
if (xiangsi > xiangsi_old)
{
xiangsi_old = xiangsi;
xiangsi_old_per = (xiangsitrue / Math.Min(CodeArray[arrayIndex].Length, code.Length));
arrayIndex_temp = arrayIndex;
arrayIndex_temp = arrayIndex_temp;
}
}
}
//0.83
if (xiangsi_old_per >= 0.83) //找到最相似
{
if (arrayIndex_temp < 10) // 0..9
singleChar = (char)(48 + arrayIndex_temp);
else if (arrayIndex_temp < 36) //A..Z
singleChar = (char)(65 + arrayIndex_temp - 10);
else
singleChar = (char)(97 + arrayIndex_temp - 36);
result = result + singleChar;
}
}
}
return result;
}
}
}
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;
using System.Net.Sockets;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.IO.Compression;
using System.Collections;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
[DllImport("Tick_O.dll", EntryPoint = "OCR")]
public static extern IntPtr OCR(string file, int type);
public string cookies;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Image Im = Image.FromFile(Application.StartupPath + @"\" + "123.bmp");
pictureBox1.Image = Im;
Bitmap Bmp = new Bitmap(Application.StartupPath + @"\" + "123.bmp");
IdentifyCodebaseXQ m_Code = new IdentifyCodebaseXQ(Bmp);
string Code = m_Code.IdentifyCode();
if (Code.Length != 4) {
button1_Click(button1, new EventArgs());
}
textBox1.Text = Code;
}
private void button2_Click(object sender, EventArgs e)
{
string sUrl = "http://202.181.212.22/web/login.action";
Regex rgx_host = new Regex("[\\S\\s]*?://([\\S\\s]*?)/[\\S\\s]*");
string host = rgx_host.Match(sUrl).Groups[1].Value;
string html = GetHtml(sUrl, out cookies);
Regex rgx_imgUrl = new Regex("<TD>验证码[\\S\\s]*?<img src='([\\S\\s]*?)'");
string strImgUrl = rgx_imgUrl.Match(html).Groups[1].Value;
//this.richTextBox1.Text = html;
byte[] b = { };
Image img = new Bitmap(GetStreamByBytes(sUrl, "http://" + host + strImgUrl, b, cookies, out cookies));//获得验证码图片
this.pictureBox2.Image = img;
//图像处理
unCodeAiYing UnCheckobj = new unCodeAiYing((Bitmap)img);
//图像灰度处理
UnCheckobj.GrayByPixels();
//去除颜色杂点()
//UnCheckobj.GrayByPixels2_fan(170);//80
UnCheckobj.GrayByPixels2(132);//80
//去除 点 及 线
UnCheckobj.ClearPicBorder(1); //80
//去除单点
//UnCheckobj.ClearNoise(255, 7, 1); //255 6
this.pictureBox3.Image = UnCheckobj.bmpobj;
//图像识别
pictureBox2.Image.Save("temp.bmp", System.Drawing.Imaging.ImageFormat.Bmp); //从指定内存地址读取字符串C#操作内存
string temp = Marshal.PtrToStringAnsi(OCR("temp.bmp", -1));
if (String.IsNullOrEmpty(temp))
{
//MessageBox.Show("请点击获得验证码");
button2_Click(button2, new EventArgs());
}
temp = temp.Replace("O", "0").Replace("o", "0").Replace("l", "1").Replace("/", "7").Replace("G", "6");
Regex reg = new Regex("\\d{4}");
MatchCollection mc = reg.Matches(temp);
if (mc.Count == 1)
{
textBox2.Text = temp.Substring(0, 4);
}
}
public static string GetHtml(string URL, out string cookie)
{
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
Stream getStream;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
//httpWebRequest.Referer = "http://www.ibcbet.com/head.aspx";
httpWebRequest.Headers.Add("Accept-Language", "zh-cn");
httpWebRequest.Headers.Add("Accept-Encoding", "gzip, deflate");
httpWebRequest.Headers.Add("L", "gzip, deflate");
//是否支持重新定向
httpWebRequest.AllowAutoRedirect = false;
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
httpWebRequest.Method = "GET";
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
cookie = webResponse.Headers.Get("Set-Cookie");
//getStream = webResponse.GetResponseStream();
getStream = Gzip((HttpWebResponse)webResponse);
//getStream = webResponse.GetResponseStream();
return new StreamReader(getStream, Encoding.UTF8).ReadToEnd();
}
public static Stream GetStreamByBytes(string server, string URL, byte[] byteRequest, string cookie,
out string header)
{
Stream stream = new MemoryStream(GetHtmlByBytes(server, URL, byteRequest, cookie, out header));
return stream;
}
public static byte[] GetHtmlByBytes(string server, string URL, byte[] byteRequest, string cookie, out string header)
{
long contentLength;
HttpWebRequest httpWebRequest;
HttpWebResponse webResponse;
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(server), cookie);
httpWebRequest.CookieContainer = co;
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Accept = "application/x-shockwave-flash, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
httpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
httpWebRequest.Headers.Add("Accept-Language", "zh-cn");
//是否支持重新定向
//httpWebRequest.AllowAutoRedirect = false;
httpWebRequest.Method = "GET";
httpWebRequest.Timeout = 15000;
httpWebRequest.ContentLength = byteRequest.Length;
webResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//header = webResponse.Headers.ToString();
header = webResponse.Headers.Get("Set-Cookie");
if (string.IsNullOrEmpty(header))
{
header = cookie;
}
else
{
header = cookie + "," + header;
}
//getStream = webResponse.GetResponseStream();
Stream gzips = Gzip((HttpWebResponse)webResponse);
contentLength = webResponse.ContentLength;
byte[] outBytes = new byte[0];
outBytes = ReadFully(gzips);
gzips.Close();
return outBytes;
}
//解密
private static Stream Gzip(HttpWebResponse HWResp)
{
Stream stream1 = null;
if (HWResp.ContentEncoding == "gzip")
{
//?
stream1 = new ICSharpCode.SharpZipLib.GZip.GZipInputStream(HWResp.GetResponseStream());
}
else
{
if (HWResp.ContentEncoding == "deflate")
{
stream1 = new ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream(HWResp.GetResponseStream());
}
}
if (stream1 == null)
{
return HWResp.GetResponseStream();
}
MemoryStream stream2 = new MemoryStream();
int count = 0x800;
byte[] buffer = new byte[0x800];
goto A;
A:
count = stream1.Read(buffer, 0, count);
if (count > 0)
{
stream2.Write(buffer, 0, count);
goto A;
}
stream2.Seek((long)0, SeekOrigin.Begin);
return stream2;
}
public static byte[] ReadFully(Stream stream)
{
byte[] buffer = new byte[128];
using (MemoryStream ms = new MemoryStream())
{
while (true)
{
int read = stream.Read(buffer, 0, buffer.Length);
if (read <= 0)
return ms.ToArray();
ms.Write(buffer, 0, read);
}
}
}
}
}