Generally it is known that frequent headaches, especially migraines, can be triggered by eating milk chocolate. It's been unclear though if it's the "milk" (lactose) that causes an allergy headache, the low % of Cocoa, or the sugar that spikes/drops blood sugar (insulin imbalance) and causes the capillaries to over-dilate.
Current research however, is citing that high % dark chocolate with high caffeine property's can actually help a headache. Caffeine constricts blood vessels and reduces blood flow allowing the brain to reduce inflammation. Purer chocolate is generally high in magnesium as well, which makes the muscles relax (which is why it's craved with cramps). Chocolate also promotes endorphins, which make you feel good.
Now clearly, there are a lot of medical pieces to this, so I'm only passing over a few. Psychologically, maybe finding the right kind of chocolate for yourself can simply relax you and make you feel good at the same time. Less stress = less headaches. Well that's my theory anyway!