I'm trying to get the picture to load in the same PHP session in which the POST request will be sent. But because i'm using button1_Click this is not possible. 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..
using 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
}
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
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
You're not going to get those two requests using the same session since they'll be coming from two different machines!
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. 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.
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. You already used a CookieContainer
so that's a good start.
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. We can then use the HttpWebResponse
to create an Image
object and assign this to the pictureBox1.Image
property.
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());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.