Java 如何在json post请求中发送字节数组?
我有一个wcf服务,它接受Java 如何在json post请求中发送字节数组?,java,json,wcf,Java,Json,Wcf,我有一个wcf服务,它接受byte[]serialData,现在正在开发一个java客户端,需要使用相同的方法 当我将bytearray作为json post请求发送到服务时,它得到了一个异常作为java.io.IOException:Server返回的HTTP响应代码:400 这是我的密码: wcf方法: [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemp
byte[]serialData
,现在正在开发一个java客户端,需要使用相同的方法
当我将bytearray作为json post请求发送到服务时,它得到了一个异常作为java.io.IOException:Server返回的HTTP响应代码:400
这是我的密码:
wcf方法:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "saveSerialNumbers", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
Dictionary<string, object> saveSerialNumbers(byte[] serialData);
for (int i = 1; i < 100; i++) {
sb.append(String.valueOf(gen()));
}
byte[] bytesEncoded = Base64.encodeBase64(sb.toString().getBytes());
String json = "{\"serialDataByte\":\""+sb.toString()+"\"}";
下面是我的方法调用:
String json = "{\"serialData\":\""+ new String(bytesEncoded) +"\",\"guProductID\":\""+guProductID+"\",\"guStoreID\":\""+guStoreID+"\",\"securityToken\":\""+SecurityToken+"\"}";
String serialContract = serialClient.getResultPOST(json, "http://localhost:3361/EcoService.svc/Json/", "saveSerialNumbers");
下面是一个从字符串实例生成json的简单工作原型。使用此代码段更新客户端部件。它应该会起作用
import java.util.Base64;
public class Test {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
// composing string to be encoded
sb.append("Part 1 of some text to be encoded to base64 format\n");
sb.append("Part 2 of some text to be encoded to base64 format\n");
sb.append("Part 3 of some text to be encoded to base64 format");
// getting base64 encoded string bytes
byte[] bytesEncoded = Base64.getEncoder().encode(sb.toString().getBytes());
// composing json
String json = "{\"serialDataByte\":\""+ new String(bytesEncoded) +"\"}";
System.out.println(json);
}
}
更新:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "saveSerialNumbers", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
Dictionary<string, object> saveSerialNumbers(byte[] serialData);
for (int i = 1; i < 100; i++) {
sb.append(String.valueOf(gen()));
}
byte[] bytesEncoded = Base64.encodeBase64(sb.toString().getBytes());
String json = "{\"serialDataByte\":\""+sb.toString()+"\"}";
代码使用Java8SDK。如果您使用的是Java8版本,那么考虑这个任务。
下面是使用Apache Commons编解码器进行Base64编码的示例代码(请注意,导入指令已更改):
更新2:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "saveSerialNumbers", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
Dictionary<string, object> saveSerialNumbers(byte[] serialData);
for (int i = 1; i < 100; i++) {
sb.append(String.valueOf(gen()));
}
byte[] bytesEncoded = Base64.encodeBase64(sb.toString().getBytes());
String json = "{\"serialDataByte\":\""+sb.toString()+"\"}";
发送POST请求时,请确保已将您的请求标记为POST请求。在提出请求之前,不要忘记这一行代码:
connection.setRequestMethod("POST");
并使用HttpURLConnection而不是URLConnection:
import java.net.HttpURLConnection;
为什么要在客户端中将base64字符串转换为字节?请尝试在客户端中不要将字符串转换为字节,也不要将字节转换为字符串。从编码器获得的字符串发送。@RafaelOsipov您的意思是作为字符串发送吗?我的意思是
string json=“{\'serialDataByte\”:\”“+新字符串(bytesEncoded)+“\”
@RafaelOsipov谢谢,我会在您的代码中尝试发送未转换的字符串,因为sb缓冲区包含未转换的字符串。在我的代码中,当您获得一个由base64编码字节组成的byte[]数组时,您可以组成一个base64字符串并将其包含到json中。代码使用Java 8 SDK。您使用的java版本是什么?仍然有效,这就是我的JSonobject{“serialData”:“MTAzNDcxMDM0NzEwMzQ3MTAzNDcxMDM0NzEwMzQ3MTAzNDcxMDM0NzEwMzQ3”,“guProductID:“123456789”,“guStoreID:“123456”,“securityToken:“57b998ef02afa6957b94bd476c00e469”}是因为我正在本地调试服务和客户端吗?这不应该是问题的原因。您是否收到“http无效请求错误”(http 400)?@rafeal yes java.io.IOException:服务器返回了URL的http响应代码:400: