首页新闻找找看学习计划

模拟html提交表单,上传图片文件到58同城

0
悬赏园豆:50 [已关闭问题] 关闭于 2014-06-12 12:31
POST http://pic.kuche.com/postpic/upload?flash=1 HTTP/1.1
Host: pic.kuche.com
Connection: keep-alive
Content-Length: 198248
Origin: http://pic2.58.com
file-extensions: jpg
pic-isaddwaterpic: True
pic-path: /p1/big/
pic-size: 640*640
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1
content-type: application/octet-stream
pic-dpi: 0
pic-cut: 0*0*0*0
pic-bulk: 0
pic-name: /p1/tiny/
Accept: */*
Referer: http://pic2.58.com/ui7/post/PictureUpload_zip_s1.swf
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3
Fiddler-Encoding: base64

上边是表头文件
下边是提交的附件Request Body,其实就是一个Base64的字符串,为了简化页面,没有全部copy上来。

/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRQBAwQEBQQFCQUFCRQNCw0UFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFP/AABEIAwAEAAMBEQACEQEDEQH/xAGiAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgsQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+gEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoLEQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2dri4+Tl5ufo6ery8/T19vf4+f

上边是通过Fidder调试出来,页面提交的表单内容,下边是我模拟表单提交的代码,请问我这有什么问题吗?一直获取不到提交后页面返回的图片路径值。

 private void PostFileTo58()
        {
//文件 
            FileStream fileStream = new FileStream(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg", FileMode.Open, FileAccess.Read);
            string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
            //请求 
            WebRequest req = WebRequest.Create(@"http://pic.kuche.com/postpic/upload?flash=1");
            req.Method = "POST";
            req.ContentType = "multipart/form-data; boundary=" + boundary;
            //组织表单数据 
            StringBuilder sb = new StringBuilder();
            sb.Append("Host: pic.kuche.com");
            sb.Append("\r\n");
            sb.Append("Connection: keep-alive");
            sb.Append("\r\n");
            sb.Append("Content-Length: 296386");
            sb.Append("\r\n");
            sb.Append("Origin: http://pic2.58.com");
            sb.Append("\r\n");
            sb.Append("file-extensions: jpg");
            sb.Append("\r\n");
            sb.Append("pic-isaddwaterpic: True");
            sb.Append("\r\n");
            sb.Append("pic-path: /p1/big/");
            sb.Append("\r\n");
            sb.Append("pic-size: 640*640");
            sb.Append("\r\n");
            sb.Append("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1");
            sb.Append("\r\n");
            sb.Append("content-type: application/octet-stream");
            sb.Append("\r\n");
            sb.Append("pic-dpi: 0");
            sb.Append("\r\n");
            sb.Append("pic-cut: 0*0*0*0");
            sb.Append("\r\n");
            sb.Append("pic-bulk: 0");
            sb.Append("\r\n");
            sb.Append("pic-name: /p1/tiny/");
            sb.Append("\r\n");
            sb.Append("Accept: */*");
            sb.Append("\r\n");
            sb.Append("Referer: http://pic2.58.com/ui7/post/PictureUpload_zip_s1.swf");
            sb.Append("\r\n");
            sb.Append("Accept-Encoding: gzip,deflate,sdch");
            sb.Append("\r\n");
            sb.Append("Accept-Language: zh-CN,zh;q=0.8");
            sb.Append("\r\n");
            sb.Append("Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3");
            sb.Append("\r\n");
            //sb.Append("Fiddler-Encoding: base64");
            sb.Append("\r\n\r\n");
            string head = sb.ToString();
            byte[] form_data = Encoding.UTF8.GetBytes(head);
            //结尾 
            byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
          
            //post总长度 
            long length = form_data.Length + fileStream.Length + foot_data.Length;
            req.ContentLength = length;

            Stream requestStream = req.GetRequestStream();
            //发送表单参数 
            requestStream.Write(form_data, 0, form_data.Length);
            //文件内容 
            byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
            int bytesRead = 0;
            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                requestStream.Write(buffer, 0, bytesRead);


            byte[] bytes = buffer;
            MemoryStream ms = new MemoryStream(bytes);
            ms.Position = 0;
            System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
            ms.Close();


            //结尾 
            requestStream.Write(foot_data, 0, foot_data.Length);
            requestStream.Close();
            string result = string.Empty;
            WebResponse wresp = null;
            try
            {
                wresp = req.GetResponse();
                Stream stream2 = wresp.GetResponseStream();
                StreamReader reader2 = new StreamReader(stream2);

                result = reader2.ReadToEnd();
            }
            catch (Exception ex)
            {
                if (wresp != null)
                {
                    wresp.Close();
                    wresp = null;
                }
            }
            finally
            {
                req = null;
            }

        }

 





就叫我好人吧的主页 就叫我好人吧 | 初学一级 | 园豆:5
提问于:2014-06-11 16:19
< >
分享
所有回答(4)
0

要我看还是用webbrowser模拟来的好一点,你直接给上传文件的那个输入框附上路径,然后点击上传按钮不就得了,用得着这么复杂么

刘宏玺 | 园豆:14004 (专家六级) | 2014-06-11 16:33

我专门查过资料,好像基于安全考虑,输入框的值是无法代码控制赋值的。

支持(0) 反对(0) 就叫我好人吧 | 园豆:5 (初学一级) | 2014-06-11 16:35

@就叫我好人吧: 额,不至于吧,他怎么区分人和机器啊,我觉得你的考虑是多余的

支持(0) 反对(0) 刘宏玺 | 园豆:14004 (专家六级) | 2014-06-11 16:37

@刘宏玺: 只是,我真的不知道怎么给浏览框里边赋值啊?能给讲下如何实现的吗?非常谢谢

支持(0) 反对(0) 就叫我好人吧 | 园豆:5 (初学一级) | 2014-06-11 16:42

@就叫我好人吧: 就和用js在dom里面赋值差不多

            HtmlElement tbname = webBrowser1.Document.All["name"];
            HtmlElement tbpassword = webBrowser1.Document.GetElementById("password");
            HtmlElement verify = webBrowser1.Document.GetElementById("verify");
            HtmlElement ImageTag = webBrowser1.Document.Images[0];
            HtmlElement submiebutton = null;
            foreach (HtmlElement element in webBrowser1.Document.GetElementsByTagName("input"))
            {
                if (element.GetAttribute("value") == "登 录")
                {
                    submiebutton = element;
                    break;
                }
            }
            tbname.SetAttribute("value", name);
            tbpassword.SetAttribute("value", password);
            verify.SetAttribute("value", BindImg(GetWebImage(webBrowser1, ImageTag)));
            submiebutton.InvokeMember("click");

上面是我以前用过的方法,你一看就能懂了

支持(0) 反对(0) 刘宏玺 | 园豆:14004 (专家六级) | 2014-06-11 16:44

@刘宏玺: 哥哥,我其实已经用webbrowser实现了58的模拟登陆,跳转及房源的发布功能,就是缺少了图片上传,webbrowser没法模拟点击“上传图片”→“选择图片”→“确认”的这个步骤

你可以看下这个页面,“图片上传”如何给它赋值?

http://post.58.com/541/8/s5

支持(0) 反对(0) 就叫我好人吧 | 园豆:5 (初学一级) | 2014-06-11 16:50

@就叫我好人吧: 

if (wbLogin.Document.GetElementById("username") != null)
wbLogin.Document.GetElementById("username").SetAttribute("value", strUserName);
if (wbLogin.Document.GetElementById("password") != null)
wbLogin.Document.GetElementById("password").SetAttribute("value", strPassWord);

if (wbLogin.Document.GetElementById("validatecode") != null)
{
Image img = CommonComponent.Framework.ValidateManage.CAPTCHA.GetWebImage(wbLogin, wbLogin.Document.GetElementById("vcodeImg"));
StringBuilder codeText = new StringBuilder();
img.Save("c:\\validate.jpg");
CommonComponent.Framework.ValidateManage.CAPTCHA.getCodeText("c73cad1f176a4e16baca70bc29b6a3f6", "c:\\validate.jpg", ref codeText); ;
wbLogin.Document.GetElementById("validatecode").SetAttribute("value", codeText.ToString());
}

wbLogin.Document.GetElementById("btnSubmit").InvokeMember("click");

 

 

我这已经实现登录和发布的功能了,上边是包含验证码的登录代码

支持(0) 反对(0) 就叫我好人吧 | 园豆:5 (初学一级) | 2014-06-11 16:53
0

已经解决了

就叫我好人吧 | 园豆:5 (初学一级) | 2014-06-12 12:30
0

怎么解决的求指导QQ:512668526

受气包 | 园豆:138 (初学一级) | 2014-07-20 03:11
0

你好!求解决思路,刚好碰到类似flash上传问题。谢谢!

无害苹果 | 园豆:202 (菜鸟二级) | 2014-09-12 17:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册