Android GET和POST请求 [英] Android GET and POST Request
问题描述
谢谢。
您可以使用 HttpURLConnection
类(在java.net中)发送POST或GET HTTP请求。它与其他任何可能需要发送HTTP请求的应用程序相同。发送Http请求的代码如下所示:
import java.net。*;
import java.io. *;
public class SendPostRequest {
public static void main(String [] args)throws MalformedURLException,IOException {
URL reqURL = new URL(http://www.stackoverflow.com/) ; //我们将发送请求到
的URL HttpURLConnection request =(HttpURLConnection)(reqUrl.openConnection());
String post =这将是您将发送的发布数据
request.setDoOutput(true);
request.addRequestProperty(Content-Length,Integer.toString(post.length)); //添加发布数据的内容长度
request.addRequestProperty(Content-Type,application / x-www-form-urlencoded); //添加请求的内容类型,大多数帖子数据是这种类型
request.setMethod(POST);
request.connect();
OutputStreamWriter writer = new OutputStreamWriter(request.getOutputStream()); //我们将在这里写入我们的请求数据
writer.write(post);
writer.flush();
}
}
GET请求看起来有点不同,但大部分代码是相同的。您不必担心使用流执行输出或指定内容长度或内容类型:
import java。净。*;
import java.io. *;
public class SendPostRequest {
public static void main(String [] args)throws MalformedURLException,IOException {
URL reqURL = new URL(http://www.stackoverflow。 COM /); //我们将发送请求到
的URL HttpURLConnection request =(HttpURLConnection)(reqUrl.openConnection());
request.setMethod(GET);
request.connect();
}
}
Can anyone point me to a good implementation of a way to send GET and POST Requests. They are alot of ways to do these, and i am looking for the best implementation. Secondly is there a generic way to send both these methods rather then using two different ways. After all the GET method merely has the params in the Query Strings, whereas the POST method uses the headers for the Params.
Thanks.
You can use the HttpURLConnection
class (in java.net) to send a POST or GET HTTP request. It is the same as any other application that might want to send an HTTP request. The code to send an Http Request would look like this:
import java.net.*;
import java.io.*;
public class SendPostRequest {
public static void main(String[] args) throws MalformedURLException, IOException {
URL reqURL = new URL("http://www.stackoverflow.com/"); //the URL we will send the request to
HttpURLConnection request = (HttpURLConnection) (reqUrl.openConnection());
String post = "this will be the post data that you will send"
request.setDoOutput(true);
request.addRequestProperty("Content-Length", Integer.toString(post.length)); //add the content length of the post data
request.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //add the content type of the request, most post data is of this type
request.setMethod("POST");
request.connect();
OutputStreamWriter writer = new OutputStreamWriter(request.getOutputStream()); //we will write our request data here
writer.write(post);
writer.flush();
}
}
A GET request will look a little bit different, but much of the code is the same. You don't have to worry about doing output with streams or specifying the content-length or content-type:
import java.net.*;
import java.io.*;
public class SendPostRequest {
public static void main(String[] args) throws MalformedURLException, IOException {
URL reqURL = new URL("http://www.stackoverflow.com/"); //the URL we will send the request to
HttpURLConnection request = (HttpURLConnection) (reqUrl.openConnection());
request.setMethod("GET");
request.connect();
}
}
这篇关于Android GET和POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!