这是我的十六进制字符串:
01312D0102D6010162000600000918000000000000000000000000007FFF00150010004B0100003A200207000004040055516200050E9E000200000000005852324531533156304330000001614B00541E0C07110507320000000000000000000000000500000000000000000406000030313030313033300000000000000000000046300A1D1655000186A0FFFF9EAA00000043000000000200000C000C000C000C000D000C000D000C000D000C000D000D000C000C000C000C000D000C000D000D000C000D000D000C000D000D000C000D000D000D000C000D000D000D000E000D000C000D000D000D000D000C000D000D000D000E000D000D000D000C000D000D029E029F02F102EA05300544085A07C1060F040A036404E0072F064804AA040404070510066B067205C304B302C6000D000D000D000E000D000D000E000D000E000D000D000E000D000D000E000D000E000D000E000D000E000D000D0065006300810081025102E001990082001F000E000D000E000D000E000E000D000E000D000E000E001500560062003300480068009000AF00AE00AE0214048404670560007E000E000E000E000E000E000F000E000E000E000E000E005501290168000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000D000E000E000D000E000D000E000E000E000D000E000E000D000E000E000D000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000E000D000E000D000E000D000E000D000E000D000E000E000D000E000D000D000D000D000D000C000D000D000E000D000D000D000D000D000D000D000E000D000D000E000D000D000E000D000D000D000D000D00E200DF00DF000F000E000D007300DFA228
将此十六进制字符串转换为具有以下代码的字节数组后,我得到一些负数值。
这是因为java中的字节已签名(-127到127)。 同意!
public static byte[] toByteArray(String s)
{
return DatatypeConverter.parseHexBinary(s);
}
由于负值,我的请求失败了,我这样说是因为当我发送值 如果我发送这样的硬代码值,那么工作正常。 我发送字节数组为 所以我需要一些工作,我可以在outputStream中发送214作为字节。
由于字节中的'D6'表示-42,我想在字节数组中发送214(-42 + 256)。 我的完整代码如下:byte arr[] = new byte[] {1,49,45,1,2,(byte)214,1,1,98,0,6,0,0,9,24,0,0,0,0,0,0,0,0,0,0,0,0,0,127,(byte)255,0,21,0,16,0,75,1,0,0,58,32,2,7,0,0,4,4,0,85,81,98,0,5,14,(byte)158,0,2,0,0,0,0,0,88,82,50,69,49,83,49,86,48,67,48,0,0,1,97,75,0,84,30,12,7,17,5,7,50,0,0,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,4,6,0,0,48,49,48,48,49,48,51,48,0,0,0,0,0,0,0,0,0,0,70,48,10,29,22,85,0,1,(byte)134,(byte)160,(byte)255,(byte)255,(byte)158,(byte)170,0,0,0,67,0,0,0,0,2,0,0,12,0,12,0,12,0,12,0,13,0,12,0,13,0,12,0,13,0,12,0,13,0,13,0,12,0,12,0,12,0,12,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,12,0,13,0,13,0,13,0,12,0,13,0,13,0,13,0,14,0,13,0,12,0,13,0,13,0,13,0,13,0,12,0,13,0,13,0,13,0,14,0,13,0,13,0,13,0,12,0,13,0,13,2,(byte)158,2,(byte)159,2,(byte)241,2,(byte)234,5,48,5,68,8,90,7,(byte)193,6,15,4,10,3,100,4,(byte)224,7,47,6,72,4,(byte)170,4,4,4,7,5,16,6,107,6,114,5,(byte)195,4,(byte)179,2,(byte)198,0,13,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,13,0,101,0,99,0,(byte)129,0,(byte)129,2,81,2,(byte)224,1,(byte)153,0,(byte)30,0,31,0,14,0,13,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,14,0,14,0,21,0,86,0,98,0,51,0,72,0,104,0,(byte)144,0,(byte)175,0,(byte)174,0,(byte)174,2,20,4,(byte)132,4,103,5,96,0,126,0,14,0,14,0,14,0,14,0,14,0,15,0,14,0,14,0,14,0,14,0,14,0,85,1,41,1,104,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,13,0,14,0,14,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,13,0,14,0,14,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,12,0,13,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,14,0,13,0,13,0,13,0,13,0,13,0,(byte)226,0,(byte)223,0,(byte)223,0,15,0,14,0,13,0,115,0,(byte) 223,(byte)162,40,38,85,115,101,114,78,97,109,101,61,101,82,101,103,38,85,115,101,114,80,97,115,115,119,111,114,100,61,97,98,99,49,50,51};
OutputStream os = con.getOutputStream();
os.write(arr);
os.flush();
os.close();
public static byte[] toByteArray(String s)
{
return DatatypeConverter.parseHexBinary(s);
}
public static void main(String[] args) throws IOException
{
String str="01312D0102D6010162000600000918000000000000000000000000007FFF00150010004B0100003A200207000004040055516200050E9E000200000000005852324531533156304330000001614B00541E0C07110507320000000000000000000000000500000000000000000406000030313030313033300000000000000000000046300A1D1655000186A0FFFF9EAA00000043000000000200000C000C000C000C000D000C000D000C000D000C000D000D000C000C000C000C000D000C000D000D000C000D000D000C000D000D000C000D000D000D000C000D000D000D000E000D000C000D000D000D000D000C000D000D000D000E000D000D000D000C000D000D029E029F02F102EA05300544085A07C1060F040A036404E0072F064804AA040404070510066B067205C304B302C6000D000D000D000E000D000D000E000D000E000D000D000E000D000D000E000D000E000D000E000D000E000D000D0065006300810081025102E001990082001F000E000D000E000D000E000E000D000E000D000E000E001500560062003300480068009000AF00AE00AE0214048404670560007E000E000E000E000E000E000F000E000E000E000E000E005501290168000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000D000E000E000D000E000D000E000E000E000D000E000E000D000E000E000D000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000E000D000E000E000D000E000E000D000E000E000E000D000E000D000E000D000E000D000E000D000E000E000D000E000D000D000D000D000D000C000D000D000E000D000D000D000D000D000D000D000E000D000D000E000D000D000E000D000D000D000D000D00E200DF00DF000F000E000D007300DFA228";
byte arr[] = convertHexToString(str);
for (int i = 0; i < arr.length; i++)
{
System.out.println(arr[i]);
}
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(arr);
os.flush();
os.close();
int responseCode = con.getResponseCode();
System.out.println("POST Response Code : " + responseCode);
System.out.println("Response Message : " + con.getResponseMessage());
System.out.println("Content Encoding : " + con.getContentEncoding());
System.out.println("Content Length : " + con.getContentLength());
System.out.println("Content Type : " + con.getContentType());
System.out.println("URL : " + con.getURL());
System.out.println("Content : " + con.getContent().toString());
System.out.println(con.getContent());
}
答案 0 :(得分:2)
HTTP响应代码500的原因很可能不是字节数组中的负值。在将其发送到服务器的过程中,这些值会自动转换为无符号值。
您很可能创建了无效的HTTP请求。使用您提供的少量源代码行很难说明究竟出了什么问题,但假设con
是HTTPUrlConnection
,您应该发送的是url编码的内容作为键值对(相同)您在?
之后在网址中看到的内容。
要找出确实出错的地方,请检查服务器的日志以查看服务器有关“内部服务器错误”原因的错误消息。