http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 8139|回复: 6

[求助] 关于查询EMS问题

[复制链接]
发表于 2013-11-9 17:20:47 | 显示全部楼层 |阅读模式
本帖最后由 myzxarp 于 2013-11-9 17:33 编辑

暂时不考虑验证码问题,采用手工填入验证码方式。

目的:查询EMS

界面如图:

运行界面

运行界面


方法如下:

打开查询页面http://www.11183.com.cn/ems/order/singleQuery_t,然后输入物流编码,然后故意输错验证码。firebug截图如下:

2.png

3.jpg

4.png

5.png

代码如下:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using DotNet.Utilities;
  9. using System.IO;


  10. namespace jitang
  11. {
  12.     public partial class FormEMS2 : Form
  13.     {
  14.         string validateurl = "http://www.ems.com.cn/ems/rand";
  15.         HttpResult hresult = new HttpResult();
  16.         HttpHelper httper = new HttpHelper();
  17.         HttpItem hitem = new HttpItem();
  18.         static string imagepath = Application.StartupPath + @"\validate.jpg";


  19.         public FormEMS2()
  20.         {
  21.             InitializeComponent();
  22.         }

  23.         private void button2_Click(object sender, EventArgs e)
  24.         {
  25.             //获取验证码图片
  26.             hitem.URL = validateurl;
  27.             hitem.ResultType = ResultType.Byte;
  28.             hresult = httper.GetHtml(hitem);
  29.             pictureBox2.Image = byteArrayToImage(hresult.ResultByte);
  30.             //获取验证码图片结束
  31.         }

  32.         /// <summary>
  33.         /// 字节数组转换为图片
  34.         /// </summary>
  35.         /// <param name="Bytes"></param>
  36.         /// <returns></returns>
  37.         private static Image byteArrayToImage(byte[] Bytes)
  38.         {
  39.             using (MemoryStream ms = new MemoryStream(Bytes))
  40.             {
  41.                 Image outputImg = Image.FromStream(ms);
  42.                 System.IO.File.Delete(imagepath);
  43.                 outputImg.Save(imagepath);
  44.                 return outputImg;
  45.             }
  46.         }

  47.         /// <summary>
  48.         /// 查询EMS
  49.         /// </summary>
  50.         /// <param name="sender"></param>
  51.         /// <param name="e"></param>
  52.         private void button3_Click(object sender, EventArgs e)
  53.         {
  54.             //查询快递开始
  55.             hitem.Postdata = string.Format("mailNum={0}&checkCode={1}", "5143344643700", textBox2.Text);
  56.             hitem.URL = "http://www.ems.com.cn/ems/order/singleQuery_t";
  57.             hitem.Cookie = hresult.Cookie.Replace("path=/,", "").Replace(";path=/", "");
  58.             hitem.ContentType = "application/x-www-form-urlencoded";
  59.             hresult = httper.GetHtml(hitem);
  60.             richTextBox2.AppendText(hresult.Html);
  61.             //查询快递结束
  62.         }
  63.     }
  64. }
复制代码
目前存在的问题:输入验证码,点击查询后,提示:
  1. <html><head><title>302 Moved Temporarily</title></head>
  2. <body bgcolor="#FFFFFF">
  3. <p>This document you requested has moved temporarily.</p>
  4. <p>It's now at <a href="http://www.11183.com.cn/mailtracking/you_jian_cha_xun.html">[url=http://www.11183.com.cn/mailtracking/you_jian_cha_xun.html%3C/a%3E.%3C/p]http://www.11183.com.cn/mailtrac ... ml</a>.</p[/url]>
  5. </body></html>
复制代码
求助各位大侠,我错在哪里了呢?




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2013-11-9 17:27:09 | 显示全部楼层
各位看官,请不吝赐教。
发表于 2013-11-9 19:54:21 | 显示全部楼层

回帖奖励 +2 金钱

这个应该是登录成功了,这个html是跳转,你带返回的cookie直接访问下试试,我休息手机不方便细看
发表于 2013-11-9 20:49:17 | 显示全部楼层
myzxarp 发表于 2013-11-9 17:27
各位看官,请不吝赐教。

帮你写下,QQ上也回复你了,其它是站长类中正则有点小问题的原因。

EMS查询.rar

66.74 KB, 下载次数: 144, 下载积分: 金钱 -1

 楼主| 发表于 2013-11-10 06:55:41 | 显示全部楼层
确实,您的代码能够运行,谢谢您。站长的帮助类中正则有问题?请指正下啊!
 楼主| 发表于 2013-11-10 08:25:35 | 显示全部楼层
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using DotNet.Utilities;
  9. using System.IO;


  10. namespace jitang
  11. {
  12.     public partial class FormEMS2 : Form
  13.     {
  14.         HttpResult hresult = new HttpResult();
  15.         HttpHelper httper = new HttpHelper();
  16.         HttpItem hitem = new HttpItem();
  17.         string cookie = string.Empty;
  18.         static string imagepath = Application.StartupPath + @"\validate.jpg";
  19.         string _Referer = string.Empty;


  20.         public FormEMS2()
  21.         {
  22.             InitializeComponent();
  23.         }

  24.         private void button2_Click(object sender, EventArgs e)
  25.         {
  26.             //获取页面cookie
  27.             hitem.URL = "http://www.ems.com.cn/ems/order/singleQuery_t";
  28.             hitem.Method = "GET";
  29.             hitem.Allowautoredirect = false;
  30.             hresult = httper.GetHtml(hitem);
  31.             _Referer = hresult.Html;
  32.             cookie = hresult.Cookie;
  33.             //获取cookie结束

  34.             //获取验证码图片
  35.             hitem.URL = "http://www.11183.com.cn/ems/rand?d=0.3501349513942344";
  36.             hitem.Method = "GET";
  37.             hitem.ResultType = ResultType.Byte;
  38.             hitem.Cookie = cookie;
  39.             hresult = httper.GetHtml(hitem);
  40.             pictureBox2.Image = byteArrayToImage(hresult.ResultByte);
  41.             //获取验证码图片结束
  42.         }

  43.         /// <summary>
  44.         /// 查询EMS
  45.         /// </summary>
  46.         /// <param name="sender"></param>
  47.         /// <param name="e"></param>
  48.         private void button3_Click(object sender, EventArgs e)
  49.         {
  50.             //查询快递开始
  51.             hitem.Method = "POST";
  52.             hitem.Postdata = string.Format("mailNum={0}&checkCode={1}", "5143344643700", textBox2.Text);
  53.             hitem.URL = "http://www.ems.com.cn/ems/order/singleQuery_t";
  54.             hitem.Cookie =cookie;
  55.             hitem.Allowautoredirect = true;
  56.             hresult = httper.GetHtml(hitem);
  57.             richTextBox2.AppendText(hresult.Html);
  58.             //查询快递结束
  59.         }

  60.         /// <summary>
  61.         /// 字节数组转换为图片
  62.         /// </summary>
  63.         /// <param name="Bytes"></param>
  64.         /// <returns></returns>
  65.         private static Image byteArrayToImage(byte[] Bytes)
  66.         {
  67.             MemoryStream ms = new MemoryStream(Bytes);
  68.             Image outputImg = Image.FromStream(ms);
  69.             System.IO.File.Delete(imagepath);
  70.             outputImg.Save(imagepath);
  71.             return outputImg;

  72.         }
  73.     }
  74. }
复制代码
代码修改了一下,仍有问题,附上有问题代码,HttpHelper为最新版本1.4.3
 楼主| 发表于 2013-11-10 08:58:53 | 显示全部楼层
  1.                             //得到返回的HTML
  2.                             if (item.URL.EndsWith("singleQuery_t") && item.Method == "GET")//这里取出跳转链接,以备后面请求的Referer用.
  3.                             {
  4.                                 result.Html = response.Headers["Location"];
  5.                             }
  6.                             else
  7.                             {
  8.                                 result.Html = encoding.GetString(ResponseByte);
  9.                             }
复制代码
为什么要这么修改呢?
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-9 09:46

© 2014-2021

快速回复 返回顶部 返回列表