我成功地使用这段代码通过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();
}
现在我可能需要通过POST
方法发送参数(即param1、param2、param3),因为它们非常长.
我在考虑向该方法添加一个额外的参数(即string httpMethod).
如何尽可能少地更改上述代码,以便能够通过GET
或POST
发送参数?
我希望这会改变
connection.setRequestMethod("GET");
到
connection.setRequestMethod("POST");
本来可以做到这一点,但参数仍然通过GET方法发送.
HttpURLConnection
有没有什么方法可以帮上忙?
任何帮助都将不胜感激.