简单的网页POST数据提交,卡住脖子的难题,400分高分求助!!

jianyuan00024 2013-10-25 09:46:39
还是这个问题

=======================================

我要通过POST的方式,输入参数提交并下载torrent文件。
地址是:
http://www.jandown.com/link.php?ref=YObPZUIwfZ


我抓取了网页数据:
code YObPZUIwfZ.torrent

我用的代码参考的:
http://blog.csdn.net/xizhibei/article/details/6991987
C#模拟POST提交表单(二)--HttpWebRequest以及HttpWebResponse

求助哪位大侠能写一段提交“YObPZUIwfZ”这个参数,就能下载到相应的YObPZUIwfZ.torrent的文件的代码!
还有怎么保存,保存到那里的问题。。。。

==================

我用的代码如下:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.jandown.com/link.php");
request.CookieContainer = new CookieContainer();
//CookieContainer cookie = request.CookieContainer;//如果用不到Cookie,删去即可
//以下是发送的http头,随便加,其中referer挺重要的,有些网站会根据这个来反盗链
request.Referer = "http://www.jandown.com";
request.Accept = "text/html, application/xhtml+xml, */*";
//request.Headers["Accept-Language"] = "zh-CN,zh;q=0.";
//request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)";
//request.KeepAlive = true;
//上面的http头看情况而定,但是下面俩必须加
request.ContentType = "multipart/form-data;";
request.Method = "POST";

Encoding encoding = Encoding.UTF8;//根据网站的编码自定义
byte[] postData = encoding.GetBytes("code=YObPZUIwfZ");//postDataStr即为发送的数据,格式还是和上次说的一样
request.ContentLength = postData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
//如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可
if (response.Headers["Content-Encoding"] != null && response.Headers["Content-Encoding"].ToLower().Contains("gzip"))
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
}

StreamReader streamReader = new StreamReader(responseStream, encoding);
string retString = streamReader.ReadToEnd();

streamReader.Close();

return retString;

=======================


版主老大给的答案是:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.jandown.com/fetch.php");
request.Referer = "http://www.jandown.com/link.php?ref=YObPZUIwfZ";
request.ContentType = "multipart/form-data; boundary=---------------------------7dd732b10abe";

var data=@"-----------------------------7dd732b10abe
Content-Disposition: form-data; name="code"

YObPZUIwfZ
-----------------------------7dd732b10abe--";

byte[] postData = encoding.GetBytes(data);//postDataStr即为发送的数据,格式还是和上次说的一样

但我没试验成功。。。。
...全文
259 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jianyuan00024 2013-10-25
  • 打赏
  • 举报
回复
feiyun0112版主大人,我用的代码如下,返回为空。。 CookieContainer cc = new CookieContainer(); HttpWebRequest request = null; HttpWebResponse response = null; string url = string.Empty; try { url = "http://www.jandown.com/fetch.php"; request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/force-download"; request.CookieContainer = cc; var data=textBox1.Text; //data = "formhash=" + formhash + "&subject=&message=" + content; string loginstr = EncodePost(data); byte[] replybyte = Encoding.UTF8.GetBytes(loginstr); request = (HttpWebRequest)WebRequest.Create(url); request.CookieContainer = cc; request.Referer = "http://www.jandown.com/link.php?ref=YObPZUIwfZ"; request.ContentType = "multipart/form-data; boundary=---------------------------7dd732b10abe"; request.Method = "POST"; request.ContentLength = replybyte.Length; Stream newStream = request.GetRequestStream(); newStream.Write(replybyte, 0, replybyte.Length); newStream.Close(); response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream, Encoding.GetEncoding("utf-8")); string sReturn = reader.ReadToEnd(); response.Close(); ================== data的数据为: -----------------------------7dd732b10abe Content-Disposition: form-data; name="code" YObPZUIwfZ -----------------------------7dd732b10abe-- 我写到textBox1.Text里了。
feiyun0112 2013-10-25
  • 打赏
  • 举报
回复
怎么个不成功?

*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
chachizhu 2013-10-25
  • 打赏
  • 举报
回复
给分吧。 调用 downit("YObPZUIwfZ"); 就可以在当前目录生成 种子文件。


        static void downit(string strKey)
        {
            string postdata = buildPostdata(strKey);
            byte[] data = Encoding.UTF8.GetBytes(postdata);
            int cacheLength = 1024;
            byte[] b = new byte[cacheLength];
            HttpWebRequest wq = (HttpWebRequest)WebRequest.Create(new Uri("http://jandown.com/fetch.php"));
            wq.Referer = "http://jandown.com";
            wq.Method = "POST";
            wq.ContentType = "multipart/form-data; boundary=---------------------------7dd3d9c1c1326";
            wq.Accept = "*/*";
            wq.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
            wq.ContentLength = postdata.Length;

            try
            {
                Stream rs = wq.GetRequestStream();
                rs.Write(data, 0, data.Length);
                //rs.Close();
                HttpWebResponse wr = (HttpWebResponse)wq.GetResponse();
                Stream ws = wr.GetResponseStream();
                FileStream sw = File.Create(strKey + ".torrent");
                int intReadlen = 0;
                while ((intReadlen = ws.Read(b, 0, cacheLength)) > 0)
                {
                    sw.Write(b, 0, intReadlen);

                }
                sw.Close();
                ws.Close();
                rs.Close();


            }
            catch (Exception e)
            {

                Console.WriteLine(e.Message);
            }

        }

        static string buildPostdata(string strKey)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("-----------------------------7dd3d9c1c1326\r\nContent-Disposition: form-data; name=\"code\"\r\n\r\n").Append(strKey).Append("\r\n")
                .Append("-----------------------------7dd3d9c1c1326\r\nContent-Disposition: form-data; name=\"action\"\r\n\r\ndownload-----------------------------7dd3d9c1c1326--");
            return sb.ToString();
        }

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧