So I thought it's time for me to learn C#, be easy with me guys, I'm very new to this.
I'm trying to create a very simple application (I'm using Windows Forms Application). My goal is:
- Using "GET" method, get the web page
- Read a text field (this value changes every time that the user is accessing the page
- Using "POST" method, send some values accordingly
Here is my code so far:
private void button2_Click(object sender, EventArgs e)
{
string URI = "http://localhost/post.php";
string myParameters = "field=value1&field2=value2";
using (WebClient wc = new WebClient())
{
string getpage = wc.DownloadString("http://localhost/post.php");
MessageBox.Show(getpage);
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
MessageBox.Show(HtmlResult);
}
}
So far so good, It's working but it's not entirely what I want to achieve here. I'm able to use POST method, but how do I use GET before sending the data? I want to send data according to GET result.
Please let me know if I should be giving a better description to what I'm trying to do.
Thanks.
Edit
This is my PHP Code:
<?php
$a = session_id();
if(empty($a))
session_start();
echo "Session: ".session_id()."<br/>\n";
Now, back to my C# code, I get different session ID in the two messages
-
Have you tried the download methods? Though your point #2 is a bit confusing. What is the ultimate goal for reading the page for a user's input. If the user inputs data don't you have it in a db?scrappedcola– scrappedcola2014年10月10日 21:28:07 +00:00Commented Oct 10, 2014 at 21:28
3 Answers 3
Reading data with GET
Please refer to this answer: Easiest way to read from a URL into a string in .NET
using(WebClient client = new WebClient()) {
string s = client.DownloadString(url);
}
Sessions
By default WebClient does not use any Session. So every call is handled as if you created a new Session. To do that you need something like this:
Please refer to these answers:
Example code
public class CookieAwareWebClient : WebClient
{
private readonly CookieContainer m_container = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
HttpWebRequest webRequest = request as HttpWebRequest;
if (webRequest != null)
{
webRequest.CookieContainer = m_container;
}
return request;
}
}
// ...
private void button2_Click(object sender, EventArgs e)
{
string URI = "http://localhost/post.php";
string myParameters = "field=value1&field2=value2";
using (WebClient wc = new CookieAwareWebClient())
{
string getpage = wc.DownloadString("http://localhost/post.php");
MessageBox.Show(getpage);
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
MessageBox.Show(HtmlResult);
}
}
7 Comments
The GET method is really nothing but the parameters passed through the address line, just send your request to string.Format("{0}?{1}", URI, myParameters) (or URI + "?" + myParameters) and simply read response.
2 Comments
PHP script. The way I understood your question is that you want to send some data to the page using a combination of GET and POST methods. If this is the case, I don't see why this would not be in the same session. But if you want to send something via GET, then based on response from the page craft another request using POST, I believe this would be a separate request. If by session you mean session in the same sense as in the browser, there is a way to handle that (cookies and what not), but if don't remember off top of my head...I know its an long time ago but :
byte[] readedData = null;
await Task.Run(async() =>
{
using (WebClient client = new WebClient())
{
client.DownloadProgressChanged += (obj, args) => progessChangedAction?.Invoke(args.ProgressPercentage);
readedData = await client.DownloadDataTaskAsync(requestUri).ConfigureAwait(false);
}
});
return readedData;
}