java使用Http协议发请求报文?
1个回答
展开全部
1.java.net.Socket 中有一个接口叫 PackageReceiver 你创建一个类继承自PackageReceiver
2.自己写一个类ConnectToHost 定义一些常量如
public ConnectToHost()
{
port = 0;
keepAlive = false;
lastCommTime = 0;
connectionCtrl = false;
maxConnection = 10;
connections = new ArrayList();
soTimeOut = 0;
inUsedConnection = 0;
alive = true;
dual = true;
reconnectImmediately = false;
pollingWeight = 1;
}
3.写一个方法用来获取本地要发送的数据,如
private static String getRequestData(String name) throws IOException {
// TODO Auto-generated method stub
File testReq = new File("E:/virturlSend/", name);
// 输入流
FileInputStream infile = null;
InputStreamReader isr = null;
BufferedReader in = null;
String strLine = "";
StringBuffer strTotalLine = new StringBuffer();
try {
infile = new FileInputStream(testReq); // 以流的方式读配置文件
isr = new InputStreamReader(infile);
in = new BufferedReader(isr);
for (int iLine = 1; (strLine = in.readLine()) != null; iLine++) { // 从上传文件中每次读一行
// System.out.println(strLine.getBytes());
strTotalLine.append(strLine);
}
} catch (Exception ee) {
} finally {
in.close();
isr.close();
infile.close();
}
return strTotalLine.toString();
// return "baoyy";
}
4.设置主机信息 如
ConnectToHost connectToHost = new ConnectToHost();
//connectToHost.setHostAddr("20.*2.*.35");
//connectToHost.setPort(27098);
connectToHost.setHostAddr("127.*.*.1");
connectToHost.setPort(6789);
connectToHost.setMaxConnection(20);
5.发送数据
service.addConnectToHost(connectToHost);
以上代码不是全部 仅提供大体思路和构架 不知道能不能帮到您
2.自己写一个类ConnectToHost 定义一些常量如
public ConnectToHost()
{
port = 0;
keepAlive = false;
lastCommTime = 0;
connectionCtrl = false;
maxConnection = 10;
connections = new ArrayList();
soTimeOut = 0;
inUsedConnection = 0;
alive = true;
dual = true;
reconnectImmediately = false;
pollingWeight = 1;
}
3.写一个方法用来获取本地要发送的数据,如
private static String getRequestData(String name) throws IOException {
// TODO Auto-generated method stub
File testReq = new File("E:/virturlSend/", name);
// 输入流
FileInputStream infile = null;
InputStreamReader isr = null;
BufferedReader in = null;
String strLine = "";
StringBuffer strTotalLine = new StringBuffer();
try {
infile = new FileInputStream(testReq); // 以流的方式读配置文件
isr = new InputStreamReader(infile);
in = new BufferedReader(isr);
for (int iLine = 1; (strLine = in.readLine()) != null; iLine++) { // 从上传文件中每次读一行
// System.out.println(strLine.getBytes());
strTotalLine.append(strLine);
}
} catch (Exception ee) {
} finally {
in.close();
isr.close();
infile.close();
}
return strTotalLine.toString();
// return "baoyy";
}
4.设置主机信息 如
ConnectToHost connectToHost = new ConnectToHost();
//connectToHost.setHostAddr("20.*2.*.35");
//connectToHost.setPort(27098);
connectToHost.setHostAddr("127.*.*.1");
connectToHost.setPort(6789);
connectToHost.setMaxConnection(20);
5.发送数据
service.addConnectToHost(connectToHost);
以上代码不是全部 仅提供大体思路和构架 不知道能不能帮到您
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询