本人新手,最近在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); }