Overview
Features
Download
Documentation
Community
Add-Ons & Services

How to access Wikipedia search?

Please post support and help requests here.

How to access Wikipedia search?

Postby DonChunior » 15 Jan 2013, 16:30

Hello together!

I assume my problem can be solved by using classes from Poco::Net, but I don't know how.
I want tho write an application that sends a string to the text field for search on http://en.wikipedia.org/wiki/Main_Page and then "pushes" the search button.
Can anybody help me with a code snippet?

Thanks in advance!
BOINC - Donate computing power instead of money
DonChunior
 
Posts: 10
Joined: 30 Oct 2012, 08:43

Re: How to access Wikipedia search?

Postby rakesh » 15 Jan 2013, 17:21

View source on the Wikipedia search page and find out the target of the form and the method (get/post). Create your HTTP request as appropriate based on the samples or unit test sources available.
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Re: How to access Wikipedia search?

Postby DonChunior » 15 Jan 2013, 17:40

Ok, the only HTML element named "form" I was able to find in the source is this one:
Code: Select all
<form action="/w/index.php" id="searchform">
   <div id="simpleSearch">
      <input name="search" title="Search Wikipedia [f]" accesskey="f" id="searchInput" />
      <button type="submit" name="button" title="Search Wikipedia for this text" id="searchButton">
         <img src="//bits.wikimedia.org/static-1.21wmf7/skins/vector/images/search-ltr.png?303-4" alt="Search" width="12" height="13" />
      </button>
      <input type='hidden' name="title" value="Special:Search"/>
   </div>
</form>

So I think the target is "/w/index.php", isn't it?
But I can't find out, if it's GET or POST.
BOINC - Donate computing power instead of money
DonChunior
 
Posts: 10
Joined: 30 Oct 2012, 08:43

Re: How to access Wikipedia search?

Postby rakesh » 15 Jan 2013, 23:58

Default action if not specified is GET
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago

Re: How to access Wikipedia search?

Postby DonChunior » 16 Jan 2013, 08:36

Despite somebody maybe can help me with a code snippet - I'm a total novice ...
BOINC - Donate computing power instead of money
DonChunior
 
Posts: 10
Joined: 30 Oct 2012, 08:43

Re: How to access Wikipedia search?

Postby rakesh » 17 Jan 2013, 06:04

The following code is from a utility function. It will need some changes for what you are doing most probably, but that is along the lines of what you need to do (note this is a regular GET request, not a POST request. Take a look at the Poco::Net unit test suite and sample applications since I think these are covered in them.

Code: Select all
HttpClientSession uma::net::createSession( const string& url )
{
  URI uri( url );
  Timespan time( DEFAULT_TIMEOUT );

  if ( ( url.find( "https://" ) != string::npos ) )
  {
    Context::Ptr context = new Context(
        Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE );
    HTTPClientSession* session = new HTTPSClientSession(
        uri.getHost(), uri.getPort(), context );
    session->setTimeout( time );
    return HttpClientSession( session );
  }
  else
  {
    HTTPClientSession* session = new HTTPClientSession( uri.getHost(), uri.getPort() );
    session->setTimeout( time );
    return HttpClientSession( session );
  }
}

bool uma::util::saveUrl( const string& filePath, const string& url,
    const bool check, const string* username, const string* password )
{
  bool status = false;

  try
  {
    URI uri( url );
    string path( uri.getPathAndQuery() );
    if ( path.empty() ) path = "/";

    if ( check )
    {
      File cf( filePath );
      if ( cf.exists() )
      {
        Timestamp fts = cf.getLastModified();
        Timestamp sts = uma::net::lastModified( url, username, password );
        if ( fts >= sts ) return true;
      }
    }

    HttpClientSession httpSession = uma::net::createSession( url );
    HTTPClientSession* session = httpSession.get();
    HTTPRequest req( HTTPRequest::HTTP_GET, path, HTTPMessage::HTTP_1_1 );

    if ( username && password )
    {
      HTTPBasicCredentials cred( *username, *password );
      cred.authenticate( req );
    }

    session->sendRequest( req );

    HTTPResponse res;
    istream& rs = session->receiveResponse( res );

    if ( res.getStatus() == HTTPResponse::HTTP_OK )
    {
      createFile( filePath );

      FileOutputStream fos( filePath, ios::out | ios::binary | ios::trunc );
      char buffer[4096];

      while ( rs.read( buffer, sizeof( buffer ) ) )
      {
        fos.write( buffer, sizeof( buffer ) );
      }

      fos.write( buffer, rs.gcount() );
      fos.close();

      status = true;
    }
    else
    {
      cout << "uma::util::saveUrl url: " << url <<
        " status: " << res.getStatus() <<
        " reason: " << res.getReason() << endl;
    }
  }
  catch ( const Exception &ex )
  {
    cerr << "uma::util::saveUrl exception: " << ex.displayText() << endl;
  }

  return status;
}
rakesh
 
Posts: 78
Joined: 13 Apr 2011, 17:43
Location: Chicago


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron