httpUTL通讯
来源:互联网 发布:手机棋牌游戏源码下载 编辑:程序博客网 时间:2024/05/01 04:23
package util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpURLConnectionTest {
public static byte[] sendHttpRequest(String url, byte[] request)
throws MalformedURLException, IOException {
HttpURLConnection urlConnection = (HttpURLConnection) new URL(url)
.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
OutputStream out = urlConnection.getOutputStream();
out.write(request);
out.flush();
out.close();
InputStream in = urlConnection.getInputStream();
ByteArrayOutputStream bais = new ByteArrayOutputStream();
byte[] bytes = new byte[2048];
int byteIn = in.read(bytes);
while (byteIn != -1) {
bais.write(bytes, 0, byteIn);
byteIn = in.read(bytes);
}
byte[] response = bais.toByteArray();
in.close();
bais.close();
urlConnection.disconnect();
return response;
}
public static void main(String args[]){
String date="你好";
try {
byte[] res = sendHttpRequest("http://192.168.1.2:8085/MOA/login?username=123&password=123", date.getBytes());
String s = new String(res);
System.out.println("============================回复数据:" + s);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- httpUTL通讯
- 串口通讯
- 多线程通讯
- 串口通讯
- 串口通讯
- 串口通讯
- 串口通讯
- “进程通讯”
- 串口通讯
- oracle通讯
- 点对点通讯
- 点对点通讯
- WinSock通讯
- socket通讯
- TCP通讯
- .Net 通讯
- Socket通讯
- 通讯聊天
- 使用compc命令来打包swc
- socket通讯 客户端和服务端
- C++写的模板类链表基本操作
- MTK手机软件系统的构建过程
- Struts2运用Uploadfy上传
- httpUTL通讯
- 2010最新SCI影响因子 - SCI检索收录期刊目录(分类目录)
- .net面试题
- 上班族最难受的5件事
- java定时器
- Flex中打包swc方法
- Event.srcElement 中属性介绍
- 读取xml配置文件
- winio的使用