Multiple WebRequest in same session
Sessions generally work by using cookies. If you want all your requests to be part of the same session, you need to persist the cookies between requests. You do this by creating a CookieContainer and providing it to each of the HttpWebRequest objects.
Here's your code updated to use a CookieContainer:
void SaveUrl(string sourceURL, string savepath) {
CookieContainer cookies = new CookieContainer();
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(sourceURL);
webRequest.CookieContainer = cookies;
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string sResponseHTML = responseReader.ReadToEnd();
using (StreamWriter sw = new StreamWriter(savepath, false)) {
sw.Write(sResponseHTML);
}
string[] ImageUrl = GetImgLinks(sResponseHTML);
foreach (string imagelink in ImageUrl) {
HttpWebRequest imgRequest = (HttpWebRequest)WebRequest.Create(imagelink);
imgRequest.CookieContainer = cookies;
HttpWebResponse imgresponse = (HttpWebResponse)imgRequest.GetResponse();
//Code to save image
}
}
multiple post requests same session c#
You need to store request.CookieContainer
in local variable and every time you need to send new request set it again
private CookieContainer cookieContainer;
private void SendRequest()
{
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
if (this.cookieContainer != null)
request.CookieContainer = this.cookieContainer;
else
request.CookieContainer = new CookieContainer();
...
...
...
this.cookieContainer = request.CookieContainer;
}
And add &
to end of postData
lines
C# Multiple http requests same PHP session
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.
Keeping a session when using HttpWebRequest
You must use a CookieContainer and keep the instance between calls.
private CookieContainer cookieContainer = new CookieContainer();
public bool isServerOnline()
{
Boolean ret = false;
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(VPMacro.MacroUploader.SERVER_URL);
req.CookieContainer = cookieContainer; // <= HERE
req.Method = "HEAD";
req.KeepAlive = false;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
if (resp.StatusCode == HttpStatusCode.OK)
{
// HTTP = 200 - Internet connection available, server online
ret = true;
}
resp.Close();
return ret;
}
catch (WebException we)
{
// Exception - connection not available
Log.e("InternetUtils - isServerOnline - " + we.Status);
return false;
}
}
Related Topics
C# Serialized JSON Date to Ruby
Wpf Webbrowser (3.5 Sp1) Always on Top - Other Suggestion to Display HTML in Wpf
Duplicate Key Exception from Entity Framework
Auto Create Database Tables from Objects, Entity Framework
What Is the Equivalent of "Case When Then" (T-Sql) with Entity Framework
Monodevelop + Naudio + Ubuntu Linux Tells Me Winmm.Dll Not Found
Configure Multiple Database Entity Framework 6
Androidapp and MySQL connection Didn't Work Connection.Open
Deploying ASP.NET Website on Linux Server
Does C# Have a Way of Giving Me an Immutable Dictionary
How to Convert Xml to JSON Using C#/Linq
How to Use HTML.Textboxfor with Input Type=Date
JSON Serialize List<Keyvaluepair<String, Object>>
Is Idependencyresolver an Anti-Pattern
String List in SQLcommand Through Parameters in C#
C# Linq to SQL: Refactoring This Generic Getbyid Method
C# Compiler Bug? Why Doesn't This Implicit User-Defined Conversion Compile
Deserialize JSON Object Sent from Android App to Wcf Webservice