For your second question read this post from @Fox ,
maybe its what you need. Its in Russian, but you can
use translator. There is an example script too >
Received HTTP code 502 from proxy after CONNECT
-

Received HTTP code 502 from proxy after CONNECT
This error appears after connected to proxies, proxies are luminati based, i don't know why this happens something if there 40% success rate, 60% is failed with this error, trying to ignore with 'Ignore Fail' but still occours, can someone please help me to fix this issue?