求助!关于网络字节转换问题
目前我这边有一个http访问的程序,对方的响应消息网络协议是这样的字段名称 类型 长度 说明
a 32位无符号整数 4 高16位表示主版本,低16位表示低版本
(目前的通信协议是高16位为0xfefd, 低16位为 0xfcfb)b 32位无符号整数 4 表示长度
c 32位无符号整数 4 类型为32位无符号整数d 32位无符号整数 4 e 16位无符号整数 2
我的程序大概是这样写的
//通过http发送一个请求
url = new URL("http://xxx.xxx.xxx/xxx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(0);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
HttpUrlLogin http =new HttpUrlLogin();
conn.getOutputStream().write("1".getBytes());//获取返回响应的字节流
InputStream in = conn.getInputStream();如何根据上面的协议得到a,b,c,d,e 的值,目前是我疑惑的地方,如果通过in一个个字节遍历循环得到的是乱码,我想应该会有字节转换的问题。但是这一块也就是需要大侠帮忙的地方
不知道代码怎么实现这些转换
目前我这边有一个http访问的程序,对方的响应消息网络协议是这样的字段名称 类型 长度 说明
a 32位无符号整数 4 高16位表示主版本,低16位表示低版本
(目前的通信协议是高16位为0xfefd, 低16位为 0xfcfb)b 32位无符号整数 4 表示长度
c 32位无符号整数 4 类型为32位无符号整数d 32位无符号整数 4 e 16位无符号整数 2
我的程序大概是这样写的
//通过http发送一个请求
url = new URL("http://xxx.xxx.xxx/xxx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(0);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
HttpUrlLogin http =new HttpUrlLogin();
conn.getOutputStream().write("1".getBytes());//获取返回响应的字节流
InputStream in = conn.getInputStream();如何根据上面的协议得到a,b,c,d,e 的值,目前是我疑惑的地方,如果通过in一个个字节遍历循环得到的是乱码,我想应该会有字节转换的问题。但是这一块也就是需要大侠帮忙的地方
不知道代码怎么实现这些转换
/**
* 发送GET请求数据
* @param get_url url地址
* @param content 发送内容 key=value形式
* @return 返回结果
* @throws Exception
*/
public String sendGetData(String get_url, String content) throws Exception {
//String result = "";
StringBuffer result=new StringBuffer("");
URL getUrl = null;
BufferedReader reader = null;
String lines = "";
HttpURLConnection connection = null;
try {
if (content != null && !content.equals(""))
get_url = get_url + "?" + content;
//get_url = get_url + "?" + URLEncoder.encode(content, "utf-8");
getUrl = new URL(get_url);
connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
// 取得输入流,并使用Reader读取
reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), "utf-8"));// 设置编码
while ((lines = reader.readLine()) != null) {
result.append(lines);
}
return result.toString();
} catch (Exception e) {
throw e;
} finally {
if (reader != null) {
reader.close();
reader = null;
}
connection.disconnect();
}
}
public String sendGetData(String get_url) throws Exception {
//String result = "";
StringBuffer result=new StringBuffer("");
URL getUrl = null;
BufferedReader reader = null;
String lines = "";
HttpURLConnection connection = null;
try {
getUrl = new URL(get_url);
connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
// 取得输入流,并使用Reader读取
reader = new BufferedReader(new InputStreamReader(connection
.getInputStream(), "utf-8"));// 设置编码
while ((lines = reader.readLine()) != null) {
result.append(lines).append("\n");
}
return result.toString();
} catch (Exception e) {
throw e;
} finally {
if (reader != null) {
reader.close();
reader = null;
}
connection.disconnect();
}
}
/**
* 发送POST请求数据
* @param POST_URL url地址
* @param content 发送内容 key=value形式
* @return 返回结果
* @throws Exception
*/
public String sendPostData(String POST_URL, String content)
throws Exception {
HttpURLConnection connection=null;
DataOutputStream out=null;
BufferedReader reader=null;
String line = "";
StringBuffer result=new StringBuffer("");
try {
URL postUrl = new URL(POST_URL);
connection= (HttpURLConnection) postUrl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
// Post 请求不能使用缓存
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
// 发送流数据
out = new DataOutputStream(connection.getOutputStream());
//content = URLEncoder.encode(content, "utf-8");
// DataOutputStream.writeBytes将字符串中的16位的unicode字符以8位的字符形式写道流里面
out.writeBytes(content);
out.flush();
out.close();
//获取结果
reader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "utf-8"));// 设置编码
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} catch (Exception e) {
throw e;
}finally
{
if(out!=null)
{
out.close();
out=null;
}
if(reader!=null)
{
reader.close();
reader=null;
}
connection.disconnect();
}
}
希望对你有帮助
你这个是通用的程序,而不是根据我说的协议来解析的
因为服务端是用c语言写的,我这边客户端用的是java来写的,
c语言有高位和地位,而且a是获取4个字节,
需要字节转换才能正常的解析,还有JAVA和C语言类型长度是有区别的
谢谢你的提供,但是没有根据我提供的协议来进行编码
获取的都是4个buf,e是2个buf,第一次肯定通过in.read(byte[4])这样方式来读4个字节,然后再通过转换方式高地位来解析,具体怎么转,这就是我的困难了请看清楚协议
目前我这边有一个JAVA 的客户端http访问的程序,对方服务端C语言的响应消息网络协议是这样的字段名称 类型 长度 说明
a 32位无符号整数 4 高16位表示主版本,低16位表示低版本
(目前的通信协议是高16位为0xfefd, 低16位为 0xfcfb)b 32位无符号整数 4 表示长度
c 32位无符号整数 4 类型为32位无符号整数d 32位无符号整数 4 e 16位无符号整数 2