Repository: intellij-rust/intellij-rust
Issue: "Failed to share the code in the Rust Playground" error in 2022.2.1

## Environment

* **IntelliJ Rust plugin version:** 0.4.177.4844-222
* **Rust toolchain version:** 1.63.0 (4b91a6ea7 2022-08-08) aarch64-apple-darwin
* **IDE name and version:** CLion 2022.2.1 (CL-222.3739.54)
* **Operating system:** macOS 12.4
* **Macro expansion engine:** new
* **Name resolution engine:** new

## Problem description

"Share in Playground" introduced in #6563 no longer work. CLion 2022.2.1 reports

> Failed to share the code in the Rust Playground

Exception raised in the log

```
2022-09-03 15:10:35,119 [  34894] SEVERE - #c.i.o.p.Task - Not a JSON Object: null
java.lang.IllegalStateException: Not a JSON Object: null
	at com.google.gson.JsonElement.getAsJsonObject(JsonElement.java:91)
	at org.rust.openapiext.JsonUtils.parseJsonObject(JsonUtils.kt:28)
	at org.rust.openapiext.JsonUtils.parseJsonObject$default(JsonUtils.kt:25)
	at org.rust.ide.actions.ShareInPlaygroundAction$Companion$performAction$1.run(ShareInPlaygroundAction.kt:85)
	at com.intellij.openapi.progress.impl.CoreProgressManager.startTask(CoreProgressManager.java:442)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.startTask(ProgressManagerImpl.java:114)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcessWithProgressAsynchronously$5(CoreProgressManager.java:493)
	at com.intellij.openapi.progress.impl.ProgressRunner.lambda$submit$3(ProgressRunner.java:252)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:188)
	at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$12(CoreProgressManager.java:608)
	at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:683)
	at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:639)
	at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:607)
	at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:60)
	at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:175)
	at com.intellij.openapi.progress.impl.ProgressRunner.lambda$submit$4(ProgressRunner.java:252)
	at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1768)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:702)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:699)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:699)
	at java.base/java.lang.Thread.run(Thread.java:833)
```

Changed files:
src/main/kotlin/org/rust/ide/actions/ShareInPlaygroundAction.kt

diff --git a/src/main/kotlin/org/rust/ide/actions/ShareInPlaygroundAction.kt b/src/main/kotlin/org/rust/ide/actions/ShareInPlaygroundAction.kt
index 509e376d5..ab67725ef 100644
--- a/src/main/kotlin/org/rust/ide/actions/ShareInPlaygroundAction.kt
+++ b/src/main/kotlin/org/rust/ide/actions/ShareInPlaygroundAction.kt
@@ -7,6 +7,7 @@ package org.rust.ide.actions
 
 import com.google.common.annotations.VisibleForTesting
 import com.google.gson.Gson
+import com.intellij.execution.process.ProcessIOExecutorService
 import com.intellij.ide.util.PropertiesComponent
 import com.intellij.notification.NotificationAction
 import com.intellij.notification.NotificationListener
@@ -22,6 +23,8 @@ import com.intellij.openapi.project.Project
 import com.intellij.openapi.ui.DialogWrapper
 import com.intellij.openapi.ui.Messages
 import com.intellij.util.io.HttpRequests
+import com.intellij.util.net.ssl.CertificateManager
+import com.intellij.util.proxy.CommonProxy
 import org.jetbrains.annotations.TestOnly
 import org.rust.RsBundle
 import org.rust.cargo.project.workspace.CargoWorkspace.Edition
@@ -31,6 +34,11 @@ import org.rust.lang.core.psi.RsFile
 import org.rust.openapiext.JsonUtils
 import org.rust.openapiext.isUnitTestMode
 import java.awt.datatransfer.StringSelection
+import java.net.URI
+import java.net.http.HttpClient
+import java.net.http.HttpRequest
+import java.net.http.HttpResponse.BodyHandlers
+import java.time.Duration
 
 class ShareInPlaygroundAction : DumbAwareAction() {
 
@@ -75,14 +83,18 @@ class ShareInPlaygroundAction : DumbAwareAction() {
                 override fun shouldStartInBackground(): Boolean = true
                 override fun run(indicator: ProgressIndicator) {
                     val json = Gson().toJson(PlaygroundCode(text))
-                    val response = HttpRequests.post("$playgroundHost/meta/gist/", "application/json")
-                        .userAgent(USER_AGENT)
-                        .connect {
-                            it.write(json)
-                            it.readString(indicator)
-                        }
-
-                    gistId = JsonUtils.parseJsonObject(response).getAsJsonPrimitive("id").asString
+                    val request = HttpRequest.newBuilder()
+                        .uri(URI.create("$playgroundHost/meta/gist/"))
+                        .header("Content-Type", HttpRequests.JSON_CONTENT_TYPE)
+                        .header("User-Agent", USER_AGENT)
+                        .timeout(Duration.ofMillis(HttpRequests.READ_TIMEOUT.toLong()))
+                        .POST(HttpRequest.BodyPublishers.ofString(json))
+                        .build()
+
+                    val client = createHttpClient()
+                    val response = client.send(request, BodyHandlers.ofString())
+
+                    gistId = JsonUtils.parseJsonObject(response.body()).getAsJsonPrimitive("id").asString
                 }
 
                 override fun onSuccess() {
@@ -112,6 +124,14 @@ class ShareInPlaygroundAction : DumbAwareAction() {
             }.queue()
         }
 
+        private fun createHttpClient(): HttpClient = HttpClient.newBuilder()
+            .followRedirects(HttpClient.Redirect.NORMAL)
+            .connectTimeout(Duration.ofMillis(HttpRequests.CONNECTION_TIMEOUT.toLong()))
+            .executor(ProcessIOExecutorService.INSTANCE)
+            .sslContext(CertificateManager.getInstance().sslContext)
+            .proxy(CommonProxy.getInstance())
+            .build()
+
         private fun confirmShare(file: RsFile, hasSelection: Boolean): Boolean {
             val showConfirmation = PropertiesComponent.getInstance().getBoolean(SHOW_SHARE_IN_PLAYGROUND_CONFIRMATION, true)
             if (!showConfirmation) {