URL url=new URL("http://10.0.2.2:8080/LoginTest/servlet/LoginServlet?username="+URLEncoder.encode("zhangsan")+"&password=123");
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");//设置方法
connection.connect();//连接
if(connection.getResponseCode()==200){
InputStream is = connection.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader bf=new BufferedReader(isr);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] by=new byte[1024];
int len=0;
while((len=is.read(by))!=-1){
bos.write(by, 0, len);
}
connect = new String(bos.toByteArray(),"utf-8");
}
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");//设置方法
connection.connect();//连接
if(connection.getResponseCode()==200){
InputStream is = connection.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader bf=new BufferedReader(isr);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] by=new byte[1024];
int len=0;
while((len=is.read(by))!=-1){
bos.write(by, 0, len);
}
connect = new String(bos.toByteArray(),"utf-8");
}
本文介绍了一个使用Java通过HTTP POST请求调用远程登录接口的例子。具体展示了如何构造URL、设置请求方法、读取响应数据等过程。
6万+

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



