[c#应用程序在没有提琴手的情况下不会发送请求

问题描述 投票:1回答:1

我已将此应用程序编码为c#。该应用程序使用httpclient和webrequests方法。

我面临的问题是,当我运行应用程序时,它实际上并未发送请求。但是,当我运行提琴手然后运行应用程序时,它会发送请求并且可以正常运行。可能是什么原因?

此发送请求。


public void Login()
        {

            Loginfirst();

            //Create cookies

            // create request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www." + server_site + ".com/login");
            //request.CookieContainer = cookieJar;

            request.Method = "POST";
            string postData = "email=" + Username + "&password=" + Password + "&remember=false&request_type=ajax";
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);


            //set the contenttype property
            request.ContentType = "application/x-www-form-urlencoded";
            request.ServicePoint.Expect100Continue = true;
            //request.AllowAutoRedirect = true;
            //set header
            request.UserAgent = User_Agent;
            request.Accept = "application/json";
            request.Headers.Add("X-Requested-with", "XMLHttpRequest");


            request.Headers.Add("Cookie", m_cookies);

            log_file("line 975" + m_cookies);


            //request.Headers["Cookie"] = "rlogin=" + Username + "";
            request.KeepAlive = false;
            //set the contentlength property of the WebRequest
            request.ContentLength = byteArray.Length;

            // get the stream containing returned by the server.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream
            dataStream.Write(byteArray, 0, byteArray.Length);
            //close the Stream object
            dataStream.Close();
            // get the response


            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                cookieCount = cookieJar.Count;
                String setcookieheader = response.Headers[HttpResponseHeader.SetCookie];

                Console.WriteLine(setcookieheader);

                foreach (Cookie cookie in response.Cookies)
                {
                    if (cookie.Name == "PHPSESSID")
                    {                        
                        PHPSESSID = cookie.Value;
                        logged = true;
                        grid_status = "Log OK";
                        log_file("Grid Status OK");
                    }
                }


                //display statuts
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);



                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Console.WriteLine("Status OK");                    
                    log_file("Status OK 1022");
                    all_other_requests("profile");
                    all_other_requests("profile_status");
                    all_other_requests("faster");

                    //all_other_requests("fasterstart");

                    real_timer.Elapsed += Real_timer_Elapsed;
                    real_timer.Interval = 1000;
                    real_timer.Enabled = true;
                    real_timer.Start();


                }
                else
                {
                    Login();

                }

                dataStream = response.GetResponseStream();
                //open the stream using a streamreader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                //read the content
                //response_object = reader.ReadToEnd().Replace("\\/", "/").Replace("\"", "\'");
                response_object = reader.ReadToEnd();
                Console.WriteLine(response_object);
                reader.Close();
                response.Close();


            }
            catch (WebException e)
            {

                log_file("Line 1055" + e.Message);

                if (e.Status == WebExceptionStatus.Timeout)
                {
                    Login();
                    return;
                }
            }


            //dynamic result = JsonConvert.DeserializeObject(response_object);

            /*
            if (result.response.result=="bad_auth")
            {
                logged = false;         
            }

            if(result.response.result == "ok_auth")
            {
                //MessageBox.Show("logged in");
                Session = result.response.session;
                logged = true;
                all_other_requests("get");
            } */



        }


t0 = new Task(bb0.Login);
                t0.Start();

    t1 = new Task(bb1.Login);
                t1.Start();
    t2 = new Task(bb2.Login);
                t2.Start();

任务t0在没有提琴手的情况下工作。没有提琴手,任务t1将无法工作。没有提琴手,任务t2将无法工作。

您一次只能成功完成一项任务。就像您运行t2一样,则无法使用t1,反之亦然。

我已将此应用程序编码为c#。该应用程序使用httpclient和webrequests方法。我面临的问题是,当我运行应用程序时,它实际上并未发送请求。但是,当我运行...

c#
1个回答
1
投票

这只会创建请求对象。您必须调用GetResponse()BeginGetResponse()才能将请求实际发送到服务器。

© www.soinside.com 2019 - 2024. All rights reserved.