This is fine and all, but I do wonder the following:
How come you're requesting the connection to be kept alive? Isn't this a one-time connection that will disconnect when you're done?
When it comes to the encoding, I would suggest the usage of
rawurlencode as that does exactly as you're algorithm does. Then better, you could use
urlencode instead as that complies with the application/x-www-form-urlencoded. Was there a certain reason for this as well?