[英]Convert to string to byte array in c# and form byte array to string in java
我有客戶端服務器設置,客戶端使用C#,服務器使用Java。 現在,我將一個字符串編碼為字節數組,然后將其發送到Java服務器,但是當Java對其進行解碼時,它將收到一個在每個字符后帶有空格的String
:
Sending PLAIN >>>>> Received P L A I N
C#代碼(發送方):
static byte[] GetBytes(string str) {
byte[] bytes = Encoding.UTF8.GetBytes(str);
// System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
Java代碼(接收方):
protected SaslResponse receiveSaslMessage() throws TTransportException {
underlyingTransport.readAll(messageHeader, 0, messageHeader.length);
byte statusByte = messageHeader[0];
// Bytes for C# code received here
byte[] payload = new byte[EncodingUtils.decodeBigEndian(messageHeader, STATUS_BYTES)];
underlyingTransport.readAll(payload, 0, payload.length);
NegotiationStatus status = NegotiationStatus.byValue(statusByte);
if (status == null) {
sendAndThrowMessage(NegotiationStatus.ERROR, "Invalid status " + statusByte);
} else if (status == NegotiationStatus.BAD || status == NegotiationStatus.ERROR) {
try {
String remoteMessage = new String(payload, "UTF-8");
throw new TTransportException("Peer indicated failure: " + remoteMessage);
} catch (UnsupportedEncodingException e) {
throw new TTransportException(e);
}
}
if (LOGGER.isDebugEnabled())
LOGGER.debug(getRole() + ": Received message with status {} and payload length {}",
status, payload.length);
return new SaslResponse(status, payload);
}
protected void handleSaslStartMessage() throws TTransportException, SaslException {
// call upper method
SaslResponse message = receiveSaslMessage();
LOGGER.debug("Received start message with status {}", message.status);
if (message.status != NegotiationStatus.START) {
sendAndThrowMessage(NegotiationStatus.ERROR, "Expecting START status, received " + message.status);
}
// Bytes converted to string here - Received P L A I N
String mechanismName = new String(message.payload);
TSaslServerDefinition serverDefinition = serverDefinitionMap.get(mechanismName);
LOGGER.debug("Received mechanism name '{}'", mechanismName);
if (serverDefinition == null) {
sendAndThrowMessage(NegotiationStatus.BAD, "Unsupported mechanism type " + mechanismName);
}
SaslServer saslServer = Sasl.createSaslServer(serverDefinition.mechanism,
serverDefinition.protocol, serverDefinition.serverName, serverDefinition.props,
serverDefinition.cbh);
setSaslServer(saslServer);
}
首先將調試添加到C#代碼中:
static byte[] GetBytes(string str) {
byte[] bytes = Encoding.UTF8.GetBytes(str);
// System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
Console.WriteLine("Base64 debug: " + Convert.ToBase64String(data));
return bytes;
}
然后將相同的代碼轉換成Java代碼:
// Bytes converted to string here - Received P L A I N
System.out.println("Base64 debug: " + new sun.misc.BASE64Encoder().encode(message.payload));
String mechanismName = new String(message.payload);
最后更換:
//String mechanismName = new String(message.payload);
String mechanismName = new String(message.payload, "UTF-8");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.