Python请求失败后返回500e

2024-06-26 00:08:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在GitHub(https://github.com/cjyoung/MouseBitesWPF)上使用这个优秀的项目。但是,这是用C编写的,我确实需要用Python编写一些东西。我已经将代码简化为它需要工作的最基本的内容,并提出了以下建议:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DisneyFinderSmaller
{
    class Program
    {

        static CookieContainer cookieJar = new CookieContainer();
        static internal string rootUrl = "https://disneyworld.disney.go.com";
        static internal string siteUrl = "/dining/";        
        static internal string diningSearchUrl = "/finder/dining-availability";


        static void Main(string[] args)
        {
            LaunchSearchInstance();
            string test = Console.ReadLine();

        }


        private static void LaunchSearchInstance()
        {
            string result = getCookiesFromRequest(rootUrl + siteUrl, "", "GET");
            string pep_csrf = "";
            Match match = Regex.Match(result, "<input[^>]*name=['\"]pep_csrf['\"][^>]*value=['\"]([^'\"]*)['\"]*[^>]>", RegexOptions.Singleline & RegexOptions.IgnoreCase);
            pep_csrf = match.Groups[1].ToString();
            ConductSearch(pep_csrf);
        }

        private static void ConductSearch(string pep_csrf)
        {


            string postString = string.Format("&searchDate={1}" +
                                                "&skipPricing=true" +
                                                "&searchTime={2}" +
                                                "&partySize={3}" +
                                                "&id={0}%3BentityType%3Drestaurant" +
                                                "&type=dining" +
                                                "&pep_csrf={4}",
                                                   "293704",
                                                    "2015-11-18",
                                                    "80000714",
                                                    "2",
                                                    pep_csrf);

            string result = getCookiesFromRequest(rootUrl + diningSearchUrl, postString, "POST");
            System.Console.WriteLine(result);

        }

        private static String getCookiesFromRequest(string url, string postString, string method = "POST")
        {
            String result = "";
            byte[] postBytes = Encoding.ASCII.GetBytes(postString);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = method;
            request.Referer = rootUrl + siteUrl;
            request.CookieContainer = cookieJar;
            if (method == "POST")
            {
                request.ContentType = "application/x-www-form-urlencoded";
                Stream postStream = request.GetRequestStream();
                postStream.Write(postBytes, 0, postBytes.Length);
                postStream.Close();
            }
            try
            {

                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
                Stream responseStream = webResponse.GetResponseStream();
                StreamReader responseStreamReader = new StreamReader(responseStream);
                result = responseStreamReader.ReadToEnd();
                responseStream.Close();
                webResponse.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("IOException source: {0}", ex.Source);
            }
            return result;
        }
     }
}

在我使用请求将其转换为Python的过程中,我得出了以下结论:

^{pr2}$

但这不起作用,并返回状态代码500。在

你觉得哪里出问题了吗?我已经把头靠在墙上好几天了,如果有任何见解,我将不胜感激。在


Tags: stringrequeststaticresultsystemcsrfpepinternal