Āfāqī Khoja challenge to Qing authority first emerged in 1797 when Sarimsaq, son of Burhān al-Dīn Khoja, attempted to launch a campaign to retake Kashghar but was stopped Narbuta Biy, the ruler of Khoqand. Attacks on the Qing began in earnest approximately twenty years later in 1820. At that time, Jāhangīr Khoja, the son of Sarimsaq, proposed to the ruler of Khoqand, Umar Khan, that they join as allies and launch a holy war against the Qing. When the proposal was rejected by Umar Khan, Jāhangīr independently led some 300 soldiers on a raid to capture Kashghar. Jāhangīr's forces clashed with the Qing, but were forced to end the expedition even before reaching the gates of the Gulbagh fortress near Kashghar. In 1825, Jāhangīr and his guerilla force ambushed and killed most every member of a small Chinese detachment. This small victory caused the local tribesmen to rally to Jāhangīr's support and shortly thereafter Jāhangīr attacked the city of Kashghar and executed the governor, a Turki. The Chinese force assigned to the area was too weak to stop the attack which expanded into a general revolt in the cities of Yangihissar, Yarkand, and Khotan where Chinese civilians caught outside the city walls were slain. When the khojas attacked, the Qing had the support of the
Hui merchants along with the Ishāqi Khojas who opposed the "debauchery" and "pillage" of the Āfāqī under Jāhangīr. Meanwhile, in Khoqand, the ruler, Madali Khan, watched the activities close to his border and made the decision to join the war in support of Jāhangīr in order to protect Khoqand trade and commerce in the region. When Jāhangīr asked for assistance in capturing the Gulbagh fortress, Madali Khan led a Khoqand army of 10,000 to Kashghar. After participating personally in the battle for 12 days, Madali Khan returned home but left part of the Khoqandian force in Jāhangīr's command. On August 27 after the Qing had exhausted their food supply, the Gulbagh fortress surrendered to Jāhangīr. That summer Jāhangīr also successfully captured Yangihissar, Yarkand, and Khotan. The Qing responded in the spring of 1827 by sending an expeditionary force of more than 20,000 soldiers to combat the Āfāqī Khoja and by the end of March had recaptured all of their lost territory. Jāhangīr was captured and taken to Peking where he was ultimately executed by
slicing (Lingchi). To protect and defend Altishahr from future attacks, the Qing increased troop levels in the territory, rebuilt the westernmost cities, and constructed stronger fortifications. Trade restrictions and boycotts were imposed against Khoqand for participating in the holy war and for allowing the Āfāqī to take refuge in Khoqand. As such, Madali Khan supported the continuation of the holy war and allowed his highest military leaders, including Haqq Quli, the overall Commander of the army, to lead a large force against the Qing. Kashghar was easily occupied by September 1830 and the invaders immediately began a siege of the Gulbagh fortress. While the Khoqandians assaulted the Gulbagh fortress, Yusuf Khoja took a large force in an attempt to capture Yarkand. At Yarkand, Chinese merchants and the Qing military declined to battle openly, taking cover inside fortifications and killing Khoqandi troops from a distance with guns and cannons. Yarkand's Turkic Muslims also helped the Qing defend against the invaders. Over the next three months, neither the Khoqandian army nor Yusuf Khoja and his partisans were able to make any further conquests. No support of any kind was received from the populace and no major rebellion supported the expedition. Eventually a Qing relief force of 40,000 arrived. By the end of December 1830, the Khoqandian army and Yusuf Khoja had retreated back to Khoqand. Rather than punish Khoqand for the invasion of 1830, the Qing realized that their former approach of trade sanctions and restrictions was ineffective at stabilizing the territory and preventing conflict. As a result, the Qing entered into an agreement with Khoqand in 1832 that normalized the relationship between the two countries first by pardoning the Kashgharians living in exile in Khoqand and the Kashgharians living in Altishahr who had supported the holy war. The Qing then compensated Khoqandi merchants for their merchandise and property losses. With regard to commerce, the Qing removed the trade sanctions that they had put in place and began treating Khoqand as a favored trading partner with special privileges related to taxes, duties and tariffs. ==Military expeditions conducted independently==