简体   繁体   English

C# 多个 http 请求相同的 PHP Z21D6F40CFB511982E442Z4E0E250A957

[英]C# Multiple http requests same PHP session

I'm trying to get the picture to load in the same PHP session in which the POST request will be sent.我正在尝试将图片加载到将发送 POST 请求的同一 PHP session 中。 But because i'm using button1_Click this is not possible.但是因为我使用的是 button1_Click 这是不可能的。 And the outcome is to get the picture to load before the data is sent.结果是在发送数据之前加载图片。 If you got any questions please ask.如果您有任何问题,请提出。

i know i go wrong with the picture loading, but i dont know exactly where..我知道我的 go 图片加载错误,但我不知道具体在哪里..

using visual c# 2010 express winforms使用视觉 c# 2010 express winforms

private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox1.ImageLocation = "http://localhost/proj/guess-my-fav/1.jpg";
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Uri uri = new Uri("http://localhost/proj/guess-my-fav/level14.php");
        var answer = textBox1.Text;
        string data = "guess=" + answer + "&level=14&time=opt";
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request.Method = WebRequestMethods.Http.Post;
        request.CookieContainer = new CookieContainer();
        request.KeepAlive = true;
        request.ContentLength = data.Length;
        request.ContentType = "application/x-www-form-urlencoded";
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(data);
        writer.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string tmp = reader.ReadToEnd();
        response.Close();
        richTextBox1.AppendText(tmp); // log - delete this line          
    }

How can i put the rendering of image under the second request?如何将图像渲染放在第二个请求下?

pictureBox1.ImageLocation = "http://localhost/proj/guess-my-fav/1.jpg";

This is going to cause the client's browser to make a request for 1.jpg这将导致客户端的浏览器请求1.jpg

Uri uri = new Uri("http://localhost/proj/guess-my-fav/level14.php");
...
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

This is going to cause the webserver running the ASP.NET website to make a request for level14.php这将导致运行 ASP.NET 网站的网络服务器请求level14.php

You're not going to get those two requests using the same session since they'll be coming from two different machines!您不会使用相同的 session 收到这两个请求,因为它们将来自两台不同的机器!

You might like to look into moving that HttpWebRequest code out of the back end, and reimplementing it on the client side as an AJAX request.您可能希望考虑将HttpWebRequest代码移出后端,并在客户端将其重新实现为 AJAX 请求。 Then both requests will be coming from the client's browser.那么这两个请求都将来自客户端的浏览器。

If you modify your code to match如果您修改代码以匹配

 private CookieContainer cookieContainer;

        private void Form1_Load(object sender, EventArgs e)
        {

            var wr = (HttpWebRequest)WebRequest.Create("http://localhost/proj/guess-my-fav/1.jpg");
            cookieContainer = new CookieContainer();
            wr.CookieContainer = this.cookieContainer;
            var resp = (HttpWebResponse)wr.GetResponse();
            wr.CookieContainer = cookieContainer;
            using (var s = resp.GetResponseStream())
            {
                pictureBox1.Image = new Bitmap(s);
            }
        }



private void button1_Click(object sender, EventArgs e)
    {
        Uri uri = new Uri("http://localhost/proj/guess-my-fav/level14.php");
        var answer = textBox1.Text;
        string data = "guess=" + answer + "&level=14&time=opt";
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request.Method = WebRequestMethods.Http.Post;
        request.CookieContainer = cookieContainer;
        request.KeepAlive = true;
        request.ContentLength = data.Length;
        request.ContentType = "application/x-www-form-urlencoded";
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(data);
        writer.Close();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string tmp = reader.ReadToEnd();
        response.Close();
        richTextBox1.AppendText(tmp); // log - delete this line          
    }

This should make both requests to the server using the same session.这应该使用相同的 session 向服务器发出两个请求。

Hope this helps.希望这可以帮助。

I am assuming here that you want to re-load the image when you click the button.我在这里假设您想在单击按钮时重新加载图像。

First of all that ImageLocation property is probably not gonna respect your session cookie, so you might have to download the image manually.首先, ImageLocation属性可能不会尊重您的 session cookie,因此您可能必须手动下载图像。 You already used a CookieContainer so that's a good start.您已经使用了CookieContainer ,所以这是一个好的开始。

What we want to do here is use a new HttpWebRequest to download the image and attach the same CookieContainer to it as this one should hold the session id after your first call.我们在这里要做的是使用一个新的HttpWebRequest下载图像并将相同的CookieContainer附加到它,因为在您第一次调用后,这个 CookieContainer 应该保存 session id。 We can then use the HttpWebResponse to create an Image object and assign this to the pictureBox1.Image property.然后我们可以使用HttpWebResponse创建一个Image object 并将其分配给pictureBox1.Image属性。

All this together might look something like this:所有这一切可能看起来像这样:

private void button1_Click(object sender, EventArgs e)
{
    Uri uri = new Uri("http://localhost/proj/guess-my-fav/level14.php");
    var answer = textBox1.Text;
    string data = "guess=" + answer + "&level=14&time=opt";

    CookieContainer cookies = new CookieContainer(); /* we want to have this for other call also

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
    request.Method = WebRequestMethods.Http.Post;
    request.CookieContainer = cookies;
    request.KeepAlive = true;
    request.ContentLength = data.Length;
    request.ContentType = "application/x-www-form-urlencoded";
    StreamWriter writer = new StreamWriter(request.GetRequestStream());
    writer.Write(data);
    writer.Close();
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string tmp = reader.ReadToEnd();
    response.Close();
    richTextBox1.AppendText(tmp); // log - delete this line 

    HttpWebRequest request2 = (HttpWebRequest)HttpWebRequest.Create("http://localhost/proj/guess-my-fav/1.jpg");
    request2.CookieContainer = cookies;
    HttpWebResponse response2 = (HttpWebResponse)request.GetResponse();
    pictureBox1.Image = Image.LoadFromStream(response2.GetResponseStream());

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM