英文:
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't get it to work.
```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
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<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();
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&username=username&password=password&scope=api&grant_type=password".getBytes(StandardCharsets.UTF_8));
}
assert conn.getResponseCode() == 200;
请注意,我已经将代码翻译成了中文,您可以使用这个中文版本的代码。
英文:
How about
import java.nio.charset.StandardCharsets
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&username=username&password=password&scope=api&grant_type=password".getBytes( StandardCharsets.UTF_8 ) )
}
assert conn.getResponseCode() == 200
答案2
得分: 1
添加 conn.setRequestProperty("Accept", "*/*")
或 conn.setRequestProperty("Accept", "application/json")
解决了我的问题。这有点奇怪,因为它在 Postman 中运行正常,而我明确没有将其放在标头中,但谁知道在幕后发生了什么。
我还相信这是与我连接的服务器相关的。似乎它不允许空的 Accept 标头,并在这种情况下返回代码 403,这真的没有什么帮助。
英文:
Adding conn.setRequestProperty("Accept", "*/*")
or conn.setRequestProperty("Accept", "application/json")
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论