Click here to Skip to main content
15,915,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys..

I would like to get some results from a website that has a search box and a button, how could i do that?

It's a news articles website in PHP and i would like to search for a value and get the titles back and use them in my c# windows application.

there are no parameters and values in the address of the website.

i have tried some things but i really don't know how to do it..

let me just mention that i am doing it for my personal use and i only want to search for one value one time a week but in an automated way and in that way when I'll have the results that i am interested of i would make my app send me an e-mail.

so far what i got from the website is this:

<form method="post" action="findnews.php">
<input align="left" size="32" name="search"/>
<input type="submit" value="new search" style="width: 80px;"/>
</form>


how could i put my own value for the search and get the results as text or something?

I am kind of a Newbie as for web coding..

I will appreciate any help..

Thank you!

Roy.

Revision:

Thanks to Abishek's advice i got someting going..
though it still seems as i have a problem.

meanwhile i am getting the source code for the page itself and not the results.

the problem may be in this line:
myWebClient.Headers.Add("search", "application/x-www-form-urlencoded");


I have tried to define this line to handle the search box on the page but i am not sure if this is the correct place to do it..

I am not really sure how to configure this line and couldn't find any material on MS sites for figuring it out..

the question now is: How do i make use of the search box?

I will appreciate if you'll have a look..
This is my C# win application..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace WinFindNews
{
    public partial class Form1 : Form
    {
        public WebHeaderCollection Headers { get; set; }
        public Form1()
        {
            InitializeComponent();

        string uriString;
            uriString = "http://thedomain.com/findnews.php";

            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();
            string postData = "test query";
            //myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            myWebClient.Headers.Add("search", "application/x-www-form-urlencoded");

            // Display the headers in the request
            richTextBox1.AppendText("Resulting Request Headers: ");
            richTextBox1.AppendText(myWebClient.Headers.ToString());

            // Apply ASCII Encoding to obtain the string as a byte array.

            byte[] byteArray = Encoding.ASCII.GetBytes(postData);
            //Console.WriteLine("Uploading to {0} ...", uriString);
            // Upload the input string using the HTTP 1.0 POST method.
            byte[] responseArray = myWebClient.UploadData(uriString, "POST", byteArray);

            // Decode and display the response.
            //Console.WriteLine("\nResponse received was {0}",
            //Encoding.ASCII.GetString(responseArray));
            richTextBox1.AppendText(Encoding.ASCII.GetString(responseArray));
        }
        }
    }


Thank you!!!

Roy.
Posted
Updated 3-Mar-10 23:06pm
v9

To me it looks like PHP is handling the search. If it does not support any RSS/atom/web services. Do a http POST request on the findnews.php with "search" field & a value to it. Check out examples of WebRequest & WebClient. So you will know how to achive this. Once you get a response, you use Regex to grab the results.

Checkout http://msdn.microsoft.com/en-us/library/system.net.webclient.headers.aspx[^] for example
 
Share this answer
 
Thanks a lot! i will try that and see where it leads me..

Thanks!!!!

Roy.

I have made something work but still have proplem, please review the question again..

Thanks!!!

Roy.
 
Share this answer
 
v3
If you simulate a web request, you will get a web response, which will not be PHP code, but it WILL Be HTML. The web server assumes you are a web browser, and so it sends HTML. You will need to parse it.

OK - it doesn't look to me like you're building your request properly. It would perhaps help if you showed us the response you're getting, but I think you need to build your post data correctly. I am not sure what format it takes, but I suspect it's variablename=value, where variablename is the name of the search box.

I am sorry, I just spotted the :mad: icon at the start of my post, I didn't mean to put that there. Sorry for any wrong impression.
 
Share this answer
 
v3
Christian, thanks for your reply.

I am perfectly fine with having html as the response, but i am not getting the response that i would want..

Thank you.

Roy.

Christian, I don't even think that the :mad: icon has shown,
everything is fine :) ..

as for the post structure and parameter, i don't really know how to set it up in the right way.. can you advice me how to do it?

Thank you!

Roy.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900