Diplomacy
Keep in mind the psychological basis of being farmed. The following things can be counted as psychological:
Joining an alliance
Your goal would be to join a well known and good alliance with which the attacker can not cope. In many cases you'll even get the stolen resources back but remember that some persons will just attack you for being in a specific alliance!
Direct contact with the farmer
A very tricky topic, there is this giant farming a dwarf, why would he want to stop? - Exactly!
There is just one reason: simple courtesy and this is what your message should look like, a friendly and nice plea to stop the farming. Keep in mind that not everybody likes "PLEAAAAAAAAAAAAAAAAAAAAAAAAAAASE" in every line but you shouldn't overdo it, either.
In the end there is no guarantee that the player will stop attacking you but in many cases this will at least help you a bit.
If you are not sure that you can write an effecient IGM, don't do it at all. The attacker might feel insulted and increase the attacks.
Contact with the alliance
Ask your own alliance for help or message the alliance of the attacker, perhaps they are willing to help you.