1、客服端代码:
package com.zwh.http;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
public class HttpUtils {
// 请求服务器端的url
private static String PATH = "http://localhost:8080/httppost/LoginServlet";
private static URL url;
public HttpUtils() {
// TODO Auto-generated constructor stub
}
static {
try {
url = new URL(PATH);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String sendPostMessage(Map<String, String> params,
String encode) {
StringBuffer buffer = new StringBuffer();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
try {
buffer.append(entry.getKey())
.append("=")
.append(URLEncoder.encode(entry.getValue(), encode))
.append("&");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
buffer.deleteCharAt(buffer.length() - 1);
System.out.println(buffer.toString());
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(3000);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod("POST");
// 获得上传信息的字节大小及长度
byte[] mydata = buffer.toString().getBytes();
// 表示设置请求体的类型是文本类型
httpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Length",
String.valueOf(mydata.length));
// 获得输出流,向服务器端输出数据
OutputStream outputStream = httpURLConnection.getOutputStream();
outputStream.write(mydata);
// 获取服务器连接的状态码
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == 200) {
return changeInputStream(
httpURLConnection.getInputStream(), encode);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "";
}
private static String changeInputStream(InputStream inputStream,
String encode) {
// TODO Auto-generated method stub
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int len = 0;
byte[] date = new byte[1024];
String result = "";
try {
while ((len = inputStream.read(date)) != -1) {
outputStream.write(date, 0, len);
}
result = new String(outputStream.toByteArray(), encode);
return result;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
Map<String, String> params = new HashMap<String, String>();
params.put("username", "admin");
params.put("password", "123");
System.out.println(sendPostMessage(params, "utf-8"));
}
}
2、服务器端代码:
package com.zwh.http;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out=response.getWriter();
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("---->username:"+username);
System.out.println("---->password:"+password);
if(username.equals("admin")&&password.equals("123")){
out.println("login is success ! ");
}else{
out.println("login is failure ! ");
}
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
3、运行结果:
客服端控制台打印:
username=admin&password=123
login is success !
服务器端控制台打印:
---->username:admin
---->password:123