问题出现在HttpURLConnection和application/x-www-form-urlencoded请求上。

huangapple go评论58阅读模式
英文:

Problem with HttpURLConnection and application/x-www-form-urlencoded request

问题

我想使用`HttpURLConnection`在Groovy中发送POST请求,但我无法使其工作。

```java-lang
HttpURLConnection conn = new URL("https://url").openConnection()
conn.setDoOutput(true)
conn.setRequestMethod("POST")
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
conn.getOutputStream().write("client_id=client_id&username=username&password=password&scope=api&grant_type=password".getBytes("UTF-8"))
assert conn.getResponseCode() == 403

我尝试过其他方法:Postman、Java OkHttp、Python请求和Java Unirest,它们都立即起作用。
这是Java Unirest的工作代码(感谢Postman):

import kong.unirest.HttpResponse

HttpResponse<String> response = Unirest.post("https://url")
        .header("Content-Type", "application/x-www-form-urlencoded")
        .field("client_id", "client_id")
        .field("username", "username")
        .field("password", "password")
        .field("scope", "api")
        .field("grant_type", "password")
        .asString();

我知道这不是一个特定的Groovy问题,但也许有人知道为什么我的HttpURLConnection版本不起作用?


<details>
<summary>英文:</summary>

I want to send a post request in Groovy using `HttpURLConnection` and I can&#39;t get it to work.

```java-lang
HttpURLConnection conn = new URL(&quot;https://url&quot;).openConnection()
conn.setDoOutput(true)
conn.setRequestMethod(&quot;POST&quot;)
conn.setRequestProperty(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
conn.getOutputStream().write(&quot;client_id=client_id&amp;username=username&amp;password=password&amp;scope=api&amp;grant_type=password&quot;.getBytes(&quot;UTF-8&quot;))
assert conn.getResponseCode() == 403

I have tried other ways: Postman, Java OkHttp, Python requests and Java Unirest and they all worked instantly.
Here is the working code for Java Unirest (credits to Postman):

import kong.unirest.HttpResponse

HttpResponse&lt;String&gt; response = Unirest.post(&quot;https://url&quot;)
        .header(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
        .field(&quot;client_id&quot;, &quot;client_id&quot;)
        .field(&quot;username&quot;, &quot;username&quot;)
        .field(&quot;password&quot;, &quot;password&quot;)
        .field(&quot;scope&quot;, &quot;api&quot;)
        .field(&quot;grant_type&quot;, &quot;password&quot;)
        .asString();

I know this is not a particular Groovy question, but maybe somebody knows why my HttpURLConnection version doesn't work?

答案1

得分: 1

Here is the code translated into Chinese:

import java.nio.charset.StandardCharsets;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.DataOutputStream;

HttpURLConnection conn = new URL("https://url").openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
new DataOutputStream(conn.getOutputStream()).withCloseable {
    it.write("client_id=client_id&amp;username=username&amp;password=password&amp;scope=api&amp;grant_type=password".getBytes(StandardCharsets.UTF_8));
}

assert conn.getResponseCode() == 200;

请注意,我已经将代码翻译成了中文,您可以使用这个中文版本的代码。

英文:

How about

import java.nio.charset.StandardCharsets

HttpURLConnection conn = new URL(&#39;https://url&#39; ).openConnection()
conn.setDoOutput( true )
conn.setRequestMethod( &quot;POST&quot; )
conn.setRequestProperty( &quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;)
new DataOutputStream(conn.getOutputStream()).withCloseable {
    it.write( &quot;client_id=client_id&amp;username=username&amp;password=password&amp;scope=api&amp;grant_type=password&quot;.getBytes( StandardCharsets.UTF_8 ) )
}

assert conn.getResponseCode() == 200

答案2

得分: 1

添加 conn.setRequestProperty("Accept", "*/*")conn.setRequestProperty("Accept", "application/json") 解决了我的问题。这有点奇怪,因为它在 Postman 中运行正常,而我明确没有将其放在标头中,但谁知道在幕后发生了什么。

我还相信这是与我连接的服务器相关的。似乎它不允许空的 Accept 标头,并在这种情况下返回代码 403,这真的没有什么帮助。

英文:

Adding conn.setRequestProperty(&quot;Accept&quot;, &quot;*/*&quot;) or conn.setRequestProperty(&quot;Accept&quot;, &quot;application/json&quot;) solved the problem for me. It's a little weird because it works with Postman, where I specifically didn't put that in the header, but who knows what's going on behind the scenes.

I also believe this is specific to the server I'm connecting to. It seems that it doesn't allow an empty Accept header and returns code 403 in that case, which isn't really helpful.

huangapple
  • 本文由 发表于 2023年5月17日 16:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定