I am successfully using this code to send HTTP
requests with some parameters via GET
method
(我已成功使用此代码通过GET
方法发送带有某些参数的HTTP
请求)
void sendRequest(String request)
{
// i.e.: request = "http://example.com/index.php?param1=a¶m2=b¶m3=c";
URL url = new URL(request);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestProperty("charset", "utf-8");
connection.connect();
}
Now I may need to send the parameters (ie param1, param2, param3) via POST
method because they are very long.
(现在,我可能需要通过POST
方法发送参数(即param1,param2,param3),因为它们很长。)
I was thinking to add an extra parameter to that method (ie String httpMethod). (我在想为该方法添加一个额外的参数(即String httpMethod)。)
How can I change the code above as little as possible to be able to send paramters either via GET
or POST
?
(如何才能尽可能地更改上面的代码,以便能够通过GET
或POST
发送参数?)
I was hoping that changing
(我希望改变)
connection.setRequestMethod("GET");
to
(至)
connection.setRequestMethod("POST");
would have done the trick, but the parameters are still sent via GET method.
(本来可以解决问题的,但是参数仍然通过GET方法发送。)
Has HttpURLConnection
got any method that would help?
(HttpURLConnection
是否有任何HttpURLConnection
方法?)
Is there any helpful Java construct? (有没有有用的Java构造?)
Any help would be very much appreciated.
(任何帮助将不胜感激。)
ask by dan translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…