java服务端 解析报文_java客户端http报文发送和解析的详细代码

本人新手,最近在java 的xml格式的http报文,希望哪位大侠给指点指点。

现给出报文格式,跪求类似发送和解析http报文的详细例子。

发送的报文格式:

POST ***********************************************

Accept: *****************************************

Referer: *****************************************

Accept-Language: *****************************************

Content-Type:*****************************************

//数据部分

RESULT:xxxxx

//参数

Parameter-Length:\r\n\r\n

ParameterKey

0

*************

*************

*************

*************

*************

*************

返回报文格式:

HTTP/1.1 200 OK

Content-Type:*************

Transfer-Encoding:*************

Connection:*************

RESULT:xxxxx

Parameter-Length: 0\r\n\r\n

代码如下:

URL url = new URL("XXXXX");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setConnectTimeout(Utils.TIMEOUT);

// conn.setReadTimeout(6000);

conn.setDoOutput(true);

conn.setRequestMethod("POST");

conn.setRequestProperty("POST",

XXXXX");

// 增加报头

conn.setRequestProperty(

"Accept",

"XXXXX");

conn.setRequestProperty("Referer",

"XXXXX");

conn.setRequestProperty("Accept-Language", "XXXXX");

.....

//数据部分

StringBuffer parameterObject = new StringBuffer();

parameterObject

.append("");

parameterObject.append("\r\n");

parameterObject.append("XXXXX");

parameterObject.append("\r\n");

parameterObject.append("XXXXX");

parameterObject.append("\r\n");

parameterObject.append("");

parameterObject.append("\r\n");

// 开启流,写入xml数据

StringBuffer data = new StringBuffer();

data.append("RESULT:xxx");

data.append("\r\n");

data.append("Parameter-Length:");

data.append(parameterObject.toString().length());

data.append("\r\n");

data.append("\r\n");

data.append(parameterObject);

// 打出发送的报文信息

Utils.LogMessage("send device info xml :\r\n" + data.toString());

OutputStream outputStream = conn.getOutputStream();

outputStream.write(data.toString().getBytes());

outputStream.flush();

outputStream.close();

if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {

return;

}

InputStream input = conn.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(

input, Utils.CHARSET));

String line = null;

StringBuffer httpRawData = new StringBuffer();

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

httpRawData.append(line).append("\r\n");

}

if (reader != null) {

reader.close();

}

if (conn != null) {

conn.disconnect();

}

// 打出接受的报文信息

String httpRawDatas = httpRawData.toString();

Utils.LogMessage("accept device info xml :\r\n" + httpRawDatas);

Map InformationMap=new HashMap();

InformationMap.put("result", findOprCode(httpRawDatas,""))

InformationMap.put(Utils.DEVICE_NUMBER, Common.getAttributeValue(httpRawDatas,

"XXX1"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// 根据http返回的数据,解析出结果 public static String findOprCode(String data, String codeName) { int codeStartIndex = data.indexOf(codeName); int codeEndIndex = data.indexOf("\\r\n", codeStartIndex); String resultCode = data.substring(codeStartIndex, codeEndIndex).trim(); return resultCode; } public static String getAttributeValue(String httpData, String attribute) { int attributeIndex = httpData.indexOf(attribute); int formerValueIndex = httpData.indexOf("", attributeIndex); int laterValueIndex = httpData.indexOf("", formerValueIndex); return httpData.substring(formerValueIndex, laterValueIndex); }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值