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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 14266|回复: 12

[求助] 400错误

[复制链接]
发表于 2013-8-22 11:16:46 | 显示全部楼层 |阅读模式
使用HttpHelper时返回如下错误:
The remote server returned an error: (400) Bad Request




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-8-22 11:21:02 | 显示全部楼层
哎,怎么你们发帖子都这么发呢?
400是怎么出现的
比如,你的代码贴下,网址,抓的包提供下,需要登录下提供账户密码,
不然我们怎么帮助你解决问题呢。
什么都不知道,
先浪费点时间问你是什么问题。
真心的建议不要着急把问题说清楚,你不说清楚问题神也帮不了你
别介意最近发这种帖子的人过多了,不是针对你一个。
 楼主| 发表于 2013-8-22 11:33:38 | 显示全部楼层
站长苏飞 发表于 2013-8-22 11:21
哎,怎么你们发帖子都这么发呢?
400是怎么出现的
比如,你的代码贴下,网址,抓的包提供下,需要登录下 ...

没提供相关信息是因为网站只能内网访问:
我把extjs提交的相关代码贴下:
Ext.Ajax.request({
               url : getRootPath() + '/login.do',
               params:{
                  username:w_user.value,
                  password:w_pass.value,
                  isAutoLogin:w_auto.checked ? 1 : 0,
                  code:w_code.value
               },



c#相关代码:
           string loginUrl = "http://xx.xx.xx.xx:8080/project";//内网ip
             string freshcodeUrl ="http://10.96.18.106:8080/project /freshcode.do";
            string dataActionUrl = "http://10.96.18.106:8080/project /job/jobsteplist.do";
            //HttpWebRequest hwr = HttpWebRequest.Create(loginUrl) as HttpWebRequest;
            //CookieContainer cc = hwr.CookieContainer;
            //hwr.CookieContainer = new CookieContainer();
            //HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();

            HttpHelper hh = new HttpHelper();
            HttpItem hi = new HttpItem() {
                URL = loginUrl,
                Method = "get",//可选项 默认为Get
                Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
                //Encoding = Encoding.Default,
                Timeout = 100000,//连接超时时间     可选项默认为100000
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
                Cookie = "",//字符串Cookie     可选项
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值
                Accept = "text/html, application/xhtml+xml, */*",//    可选项有默认值
                ContentType = "text/html",//返回类型    可选项有默认值
            };
            HttpResult hr = hh.GetHtml(hi);
            string cookies = hr.Cookie;
            logger.InfoFormat("cookies:{0}", cookies);//这个地方打印的结果是空

            bool loginFail=true;
            string verifyCode;
            while (loginFail)
            {
                hi = new HttpItem()
                {
                    URL = freshcodeUrl,
                    Method = "get",
                    Encoding = null,
                    Timeout = 10000,
                    ReadWriteTimeout = 30000,
                    IsToLower = false,
                    Cookie = cookies,
                    UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
                    Accept = "text/html, application/xhtml+xml, */*",
                    ContentType = "text/html",
                    ResultType = ResultType.Byte,
                };
                hr = hh.GetHtml(hi);
                MemoryStream ms = new MemoryStream(hr.ResultByte);
                Bitmap bitmap = new Bitmap(ms);
                verifyCode=SecurityCodeHelper.GetSecurityCode(bitmap);
                logger.InfoFormat(verifyCode);

                hi = new HttpItem()
                {
                    URL = "http://xx.xx.xx.xx:8080/project /login.do",//提交给这个地址时报错
                    Method = "post",
                    Encoding = null,
                    Timeout = 10000,
                    ReadWriteTimeout = 30000,
                    IsToLower = false,
                    Cookie = cookies,
                    UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
                    Accept = "text/html, application/xhtml+xml, */*",
                    ContentType = "text/html",
                    ResultType=ResultType.String,
                };
                hi.Postdata =string.Format( "username=test&password=test&isAutoLogin=0&code={0}",verifyCode);
                hr = hh.GetHtml(hi);//这个地方报错

发表于 2013-8-22 11:40:50 | 显示全部楼层
你的Extjs是站内访问,使用HttpHelper机制是不一样的, 要抓包才能看出来。
没有包无法分析呀
 楼主| 发表于 2013-8-22 11:44:51 | 显示全部楼层
站长苏飞 发表于 2013-8-22 11:40
你的Extjs是站内访问,使用HttpHelper机制是不一样的, 要抓包才能看出来。
没有包无法分析呀

抓包是否是取这些数据:
        Encoding = null,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
                //Encoding = Encoding.Default,
                Timeout = 100000,//连接超时时间     可选项默认为100000
                ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
                Cookie = "",//字符串Cookie     可选项
                UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统     可选项有默认值
                Accept = "text/html, application/xhtml+xml, */*",//    可选项有默认值
                ContentType = "text/html",//返回类型    可选项有默认值

除了这些还需要那些,我用httpwatch去看看
发表于 2013-8-22 12:06:25 | 显示全部楼层
httpwatch Fiddler 或者是Firbug都行
 楼主| 发表于 2013-8-22 15:38:10 | 显示全部楼层
本帖最后由 天山明月 于 2013-8-22 15:40 编辑
站长苏飞 发表于 2013-8-22 12:06
httpwatch Fiddler 或者是Firbug都行
现在的情况是cookies丢失问题,从日志看cookies2、cookies3都没有信息。

重新整理了下:
httpwatch抓包情况:

Send:
GET /jms/login.jsp HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel, */*
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.3; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Host: x.x.x.x:8080
Connection: Keep-Alive

Receive:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=1DD543CD1D5B1826EE0EF55183A00353; Path=/jms/; HttpOnly
Cache-Control: no-store
Expires: Wed, 31 Dec 1969 23:59:59 GMT
Pragma: no-cache
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 22 Aug 2013 04:07:59 GMT

2000


<html>
<head>
<meta charset="utf-8"/>
<title>Job Management System</title>
<!-- <link rel="shortcut icon" href="/jms/common/image/favicon.ico" type="image/x-icon" /> -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Expires" content="0">
<link rel="stylesheet" type="text/css" href="ext4.0/resources/css/ext-all.css" />
<link rel="stylesheet" href="/jms/common/css/login.css" />
<script type="text/javascript" src="/jms/common/js/common.js"></script>
<script type="text/javascript" src="/jms/ext4.0/ext-all.js"></script>




Send:
GET /jms/freshcode.do HTTP/1.1
Accept: */*
Referer: http://x.x.x.x:8080/jms/login.jsp
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.3; .NET4.0C; .NET4.0E)
Accept-Encoding: gzip, deflate
Host: x.x.x.x:8080
Connection: Keep-Alive
Cookie: JSESSIONID=1DD543CD1D5B1826EE0EF55183A00353


Receive:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: image/jpeg;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 22 Aug 2013 04:12:31 GMT



login:

Send:
POST /jms/login.do HTTP/1.1
Accept: */*
Accept-Language: zh-cn
Referer: http://x.x.x.x:8080/jms/login.jsp
x-requested-with: XMLHttpRequest
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.3; .NET4.0C; .NET4.0E)
Host: x.x.x.x:8080
Content-Length: 57
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=1DD543CD1D5B1826EE0EF55183A00353

username=test&password=test&isAutoLogin=0&code=6gsx

Receive:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 4
Date: Thu, 22 Aug 2013 04:16:28 GMT

True





代码:
[code=csharp]            string loginUrl = "http://x.x.x.x:8080/jms/login.jsp";
            string freshcodeUrl = "http://x.x.x.x:8080/jms/freshcode.do";
            string postUrl = "http://x.x.x.x:8080/jms/login.do";
            string dataActionUrl = "http://x.x.x.x:8080/jms/job/jobsteplist.do";

            HttpHelper hh = new HttpHelper();
            bool loginFail = true;
            string verifyCode;
            while (loginFail)
            {
                HttpItem hi = new HttpItem()
                {
                    URL = loginUrl,
                    Method = "get",//可选项 默认为Get
                    Encoding = Encoding.UTF8,//编码格式(utf-8,gb2312,gbk)     可选项 默认类会自动识别
                    //Timeout = 100000,//连接超时时间     可选项默认为100000
                    ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
                    //IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
                    Cookie = "",//字符串Cookie     可选项
                    UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.3; .NET4.0C; .NET4.0E",//用户的浏览器类型,版本,操作系统     可选项有默认值
                    Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-powerpoint, application/msword, application/vnd.ms-excel, */*",//    可选项有默认值
                    ContentType = "text/html",//返回类型    可选项有默认值
                };
                HttpResult hr = hh.GetHtml(hi);
                string cookies = hr.Cookie;
                logger.InfoFormat("Cookies1:{0}", cookies);



                hi = new HttpItem()
                {
                    URL = freshcodeUrl,
                    Method = "get",
                    Encoding = Encoding.UTF8,
                    Cookie = cookies,
                    UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.3; .NET4.0C; .NET4.0E)",
                    Accept = "*/*",
                    ContentType = "image/jpeg",
                    Referer = "http://x.x.x.x:8080/jms/login.jsp",
                    ResultType = ResultType.Byte,
                };
                hr = hh.GetHtml(hi);
                //cookies = hr.Cookie;
                logger.InfoFormat("Cookies2:{0}", hr.Cookie);
                MemoryStream ms = new MemoryStream(hr.ResultByte);
                Bitmap bitmap = new Bitmap(ms);
                verifyCode = SecurityCodeHelper.GetSecurityCode(bitmap);
                logger.InfoFormat(verifyCode);


                hi = new HttpItem()
                {
                    URL = postUrl,
                    Method = "post",
                    Encoding = Encoding.UTF8,
                    Cookie = cookies,
                    UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.3; .NET4.0C; .NET4.0E)",
                    Accept = "*/*",
                    ContentType = "application/x-www-form-urlencoded",
                    Referer = "http://x.x.x.x:8080/jms/login.jsp",
                    ResultType = ResultType.String,
                    PostDataType = PostDataType.String,
                    Allowautoredirect = true,
                };
                hi.Postdata = string.Format("username=test&password=test&isAutoLogin=0&code={0}", verifyCode);


                hr = hh.GetHtml(hi);
                cookies = hr.Cookie;//login successfully
                logger.InfoFormat("Cookies3:{0}", cookies);
                logger.InfoFormat("StatusCode:{0}", hr.StatusCode);

                string htmlStr = hr.Html;
                logger.InfoFormat(htmlStr);
                if (htmlStr.IndexOf("key") > -1)
                {
                    logger.InfoFormat("Login Successfully");
                    loginFail = false;
                }//judge login
            }[/code]
发表于 2013-8-22 16:18:04 | 显示全部楼层
CooKie使用登录后的Cookie就行 了,不管那一步的都使用登录后的。没有说明登录失败了吧。
 楼主| 发表于 2013-8-22 17:16:10 | 显示全部楼层
站长苏飞 发表于 2013-8-22 16:18
CooKie使用登录后的Cookie就行 了,不管那一步的都使用登录后的。没有说明登录失败了吧。

是不是:                hr = hh.GetHtml(hi);
                cookies = hr.Cookie;//login successfully
                logger.InfoFormat("Cookies3:{0}", cookies);
                logger.InfoFormat("StatusCode:{0}", hr.StatusCode);//这个地方返回OK就代表提交成功了


发表于 2013-8-22 17:17:48 | 显示全部楼层
提交成功之后返回的页面应该的提示吧,有提示你就取一下页面内容进行判断,这样更精确一些,Cookie并不代表一定成功
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-5-2 20:45

© 2014-2021

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