IdHttp:尝试下载文件时重定向到错误页面,__ viewstate每次更改

时间:2016-07-14 04:58:38

标签: delphi delphi-xe indy indy10 idhttp

我已经使用idHttp.post()成功登录并发布了来自2个站点的数据和下载数据,但我遇到了第三个问题 在这个新站点中,登录确实有效,但是当我尝试下载文件(使用__doPostBack作为下载链接)时,我被重定向到错误页面 我有双重和三重检查所有的帖子数据,它们就像http分析器给我看的那样我注意到的唯一区别是在我成功访问的其他尝试和网站中,__ viewstate每次都是相同的,它永远不会更改,但在第三个站点,它随每次登录而变化(我的意思是当我手动进入站点并检查http分析器结果时,我可以看到__viewstate参数值每次都不同)
我该怎么办?更改__viewstate参数的问题是什么?如果是这样我该如何解决?
我用来发布的代码:

try
 Response := TMemoryStream.Create;
try
  Request := TStringList.Create;
  try
    Request.Assign(TATDFileUtility.convertPairValueToRequestList(TATDFileUtility.extractPairValue('the site login parameters.txt', 3)));
    IdHTTP := TIdHTTP.Create;
    try
      IdHTTP.AllowCookies := True;
      IdHTTP.HandleRedirects := True;
      IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';

      IdHTTP.Post('thesite, the address for the login and main page and download page is the same', Request, Response);
      Response.SaveToFile('responseCode0.txt');

      Request.Clear;
      Response.Clear;
      Request.Assign(TATDFileUtility.convertPairValueToRequestList(TATDFileUtility.extractPairValue('httpDownloadParamters.txt', 3)));
      IdHTTP.Post('thesite, the address for the login and main page and download page is the same', Request, Response);
      Response.SaveToFile('responseCode1.txt');

正如您在检查repsonsecode0后看到的,我可以看到我已登录,但第二个响应代码显示错误并跟踪它显示我被重定向到错误页面。

1 个答案:

答案 0 :(得分:2)

ViewState是动态的。您需要首先public ActionResult Create([Bind(Include = "Title,Msg,Price,PostCode,MainPhotoPath")] PostViewModel post, HttpPostedFileBase image, int cId, int subId) { Post newPost = new Post(); //var cId = Request.Form["CategoryModel"]; //var subId = Request.Form["SubCategoryID"]; newPost.CategoryID = Convert.ToInt32(cId); newPost.SubCategoryID = Convert.ToInt32(subId); newPost.Title = post.Title; newPost.Msg = post.Msg; newPost.Price = post.Price; newPost.PostCode = post.PostCode; newPost.PostedAt = DateTime.Now; newPost.Active = false; newPost.ApplicationUserId = User.Identity.GetUserId(); newPost.CityName = Server.HtmlEncode(Request.Cookies["location"].Value); Debug.WriteLine(Request.Files.Count); HttpPostedFileBase file = Request.Files[0]; string fileName = file.FileName; int fileSize = file.ContentLength; if (fileSize >= 10) { string path = Server.MapPath("~/Upload_Files/images/"); newPost.MainPhotoPath = "~/Upload_Files/images/" + fileName; if (!Directory.Exists(path)) { Debug.WriteLine("Creating new directory"); Directory.CreateDirectory(path); } file.SaveAs(Server.MapPath("~/Upload_Files/images/" + fileName)); } else { newPost.MainPhotoPath = "~/Images/no_image.png"; } if (ModelState.IsValid) { db.Posts.Add(newPost); db.SaveChanges(); return RedirectToAction("Index", "Home"); } return View("Create", post); } 定义通常在浏览器中提交PostBack的GET元素的HTML页面。这允许Web服务器生成当前的ViewState。然后解析HTML以提取<form><input>元素的名称和值,包括ViewState,然后您可以<form>将这些值POST添加到{{1}中指定的URL {}} <form>属性。这就是Web浏览器通常所做的事情,以及您需要使用action模拟的内容。