C#多个http请求相同的PHP会话

时间:2011-05-26 12:32:55

标签: c# php http httprequest

我正在尝试将图片加载到将发送POST请求的同一PHP会话中。 但因为我使用button1_Click这是不可能的。 结果是在发送数据之前加载图片。 如果您有任何疑问,请询问。

我知道我的图片加载出错了,但我不确切知道在哪里..

使用visual 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          
    }

如何在第二次请求下放置图像?

3 个答案:

答案 0 :(得分:1)

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

这将导致客户端的浏览器发出1.jpg

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

这将导致运行ASP.NET网站的Web服务器请求level14.php

你不会使用相同的会话获得这两个请求,因为它们将来自两台不同的机器!

您可能希望将HttpWebRequest代码移出后端,并在客户端重新实现它作为AJAX请求。然后这两个请求都将来自客户端的浏览器。

答案 1 :(得分:0)

如果修改代码以匹配

 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          
    }

这应该使用相同的会话向服务器发出两个请求。

希望这有帮助。

答案 2 :(得分:0)

我假设您在此处想要在单击按钮时重新加载图像。

首先,ImageLocation属性可能不会尊重您的会话cookie,因此您可能需要手动下载图像。您已使用CookieContainer,这是一个良好的开端。

我们在这里要做的是使用新的HttpWebRequest下载图片并将相同的CookieContainer附加到该图片,因为这个图片应该在您第一次调用后保留会话ID。 然后,我们可以使用HttpWebResponse创建一个Image对象,并将其分配给pictureBox1.Image属性。

所有这些可能看起来像这样:

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());

}