使用Java发送HTTP发送POST、GET请求
在使用Java编程的时候,经验要发送POST、GET请求,但有时候还经常忘记,那好吧做个记忆的备份。
具体是POST还是GET只在setRequestMethod之间。
public static String sendPost(String url, String data){
try {
URL realUrl = new URL(url);//通过传入的url创建URL类,记得一定是http开头的。
HttpURLConnection httpConn = (HttpURLConnection)realUrl.openConnection();
httpConn.setDoOutput(true);//需要输入,输入就是我们要上传服务器的数据。
httpConn.setDoInput(true);//需要输出,输出的就是我们上传数据给服务器后服务器会给我们返回什么。
httpConn.setUseCaches(false); //不允许缓存
httpConn.setRequestMethod("POST");//微妙之处就在这里,写啥就是啥。
/*下面这几句就是设置Header的地方,*/
httpConn.setRequestProperty("accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
httpConn.setRequestProperty("Charset", "UTF-8");
/*获得输出流,这个流就是输出给服务器的*/
DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
dos.writeBytes(data);//填写输出的数据。
dos.flush();
dos.close();
/*这里等待服务器的回复信息,resultCode就是200、 302、 404、 500啥的*/
int resultCode = httpConn.getResponseCode();
/*看这里只有在HTTP_OK的时候才读取数据。因为这个才是一个真确的服务器返回*/
if(HttpURLConnection.HTTP_OK == resultCode){
StringBuffer sb=new StringBuffer();
String readLine=new String();
BufferedReader responseReader=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
while((readLine=responseReader.readLine())!=null){
sb.append(readLine).append("\n");
}
responseReader.close();
System.out.println(sb.toString());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
好了记忆备份结束。就这样结束吧。
本文介绍了一个简单的Java方法来发送POST请求。该方法包括创建URL实例、设置连接参数、定义请求属性、发送数据并读取服务器响应的过程。
1099

被折叠的 条评论
为什么被折叠?



