java http 客户端 服务端_java http请求的服务端和客户端

OutputStream opt = response.getOutputStream();

String name = request.getParameter("name");

String age = request.getParameter("age");

System.out.println("========"+name+"=="+age);

opt.write((name+"=="+age).getBytes("GBK")); //给调用方响应数据

opt.flush();

opt.close();

---------------------------------------------------------------------------------------

接收请求的方式2: 流的方式

int leng = 0;

byte[] buf = new byte[1024];

ByteArrayOutputStream out1 = new ByteArrayOutputStream();

OutputStream opt = response.getOutputStream();

InputStream in = request.getInputStream();

while ((leng = in.read(buf))!=-1) {

out1.write(buf, 0, leng);

out1.flush();

}

in.close();

System.out.println(new String(out1.toByteArray()));

opt.write((name+"=="+age).getBytes("GBK")); //给调用方响应数据

客户端:

public class Demo {

public static void main(String[] args) {

URL url = null;

HttpURLConnection conn = null;

BufferedReader readInfo = null;

StringBuffer returnXml =new StringBuffer();

OutputStream out = null;

try {

url = new URL("http://10.100.16.236:8080/lftest/servlet/SendXml");

conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(1200);

conn.setReadTimeout(1200);

//conn.setRequestProperty("content-type", "text/plain");

conn.setRequestMethod("POST");

conn.setUseCaches(false); // 忽略缓存

conn.setDoOutput(true); // 使用 URL 连接进行输入

conn.setDoInput(true); // 使用 URL 连接进行输入

StringBuffer sb = new StringBuffer();

out = conn.getOutputStream();

String age = "ffsdfdsfdsfdsf";

out.write(("name=123&age="+age+"").getBytes("GBK"));

out.flush();

out.close();

if (HttpURLConnection.HTTP_OK == conn.getResponseCode()) {// 正确返回

readInfo = new BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"GBK"));

String line = null;

while ((line = readInfo.readLine()) != null) {

returnXml.append(line);

}

System.out.println(conn.getResponseCode());

System.out.println(returnXml.toString());

}

}catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally{

conn.disconnect();

}

}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值