在RestSharp和nanohttpd

时间:2019-05-17 06:11:06

标签: java c# restsharp nanohttpd

我需要使用REST API在Windows PC和Android设备之间传输字节数组(二进制)数据。 因此,我在Windows中选择了RestSharp,在Android中选择了NonoHTTPD。

我成功发送了数据,但接收到的数据与此不同。

我发送了{0xA0、0x0A,0x00、0x30、0x00、0x00、0x00、0x00、0x00、0x00}。

NanoHTTPD收到了{0xEF,0xBF,0xBD,0x0A,0x00、0x30、0x00、0x00、0xEF,0xBF,0xBD}。

RestSharp收到了{0x3F,0x0A,0x00、0x30、0x00、0x00、0x00、0x00、0x00、0x00}。

RestSharp代码::

private void TestSend()
{
    var client = new RestClient(BaseUrl);
    var request = new RestRequest(Method.POST);

    byte[] arrBytes = new byte[10];

    arrBytes[0] = 0xA0;
    arrBytes[1] = 0x0A;
    arrBytes[2] = 0x00;
    arrBytes[3] = 0x30;
    arrBytes[4] = 0x00;
    arrBytes[5] = 0x00;
    arrBytes[6] = 0xC0;
    arrBytes[7] = 0x00;
    arrBytes[8] = 0x00;
    arrBytes[9] = 0x00;

    request.AddHeader("cmd", "PCR");
    request.AddHeader("Content-Type", "application/octet-stream; charset=utf-8");
    request.RequestFormat = DataFormat.None;
    request.AddParameter("application/octet-stream", arrBytes, ParameterType.RequestBody);

    ExecuteAndGetContent(request, MyCallBack);
}

public void ExecuteAndGetContent(RestRequest request, Action<string> callback)
{
    var client = new RestClient(BaseUrl);
    client.ExecuteAsync(request, response =>
        {
            callback(response.Content);
        });
}

private void MyCallBack(string msg)
{
    byte[] bytes = Encoding.ASCII.GetBytes(msg);
    System.Diagnostics.Trace.WriteLine(string.Format("bytes.Length = {0} ", bytes.Length));
    for (int n = 0; n < bytes.Length; ++n)
    {
        System.Diagnostics.Trace.WriteLine(string.Format("bytes[{0:00}] = {1:X2}, {2} ", n, bytes[n], (char)bytes[n]));
    }
}

NanoHTTPD代码::

@Override
public Response handle(IHTTPSession session) {
    Map<String, String> header = session.getHeaders();
    Map<String, List<String>> params = session.getParameters();
    String uri = session.getUri();
    byte[] bytesReturn = new byte[512];
    int lenReturn = 0;

    Method method = session.getMethod();
    int ContentLength = Integer.parseInt(session.getHeaders().get( "content-length" ));

    if (Method.POST.equals(method)) {

        Map<String,String> files = new HashMap<String,String>();
        String postData = "";

        try {
            session.parseBody(files);

            Iterator<String> e = files.keySet().iterator();
            while (e.hasNext()) {
                String value = e.next();
                System.out.println("  files: '" + value + "' = '" + files.get(value) + "'");
            }
        } catch (IOException ioe) {
             ioe.printStackTrace();
            //return new Response(Response.Status.INTERNAL_ERROR, MIME_PLAINTEXT, "SERVER INTERNAL ERROR: IOException: " + ioe.getMessage());
        } catch (ResponseException re) {
            //return new Response(re.getStatus(), MIME_PLAINTEXT, re.getMessage());
            re.printStackTrace();
        }

        if(session.getHeaders().get( "cmd" ).equals("PCR")) {
            byte[] buf = new byte[ContentLength];
            try {
                InputStream is = session.getInputStream();
                Log.d(TAG,String.format("is.available() = %d", is.available()));
                while(is.available() > 0) {
                    int i = is.read(buf,0,buf.length);
                    Log.d(TAG, String.format("i = %d", i));
                    if(i < 0) {
                        break;
                    }
                }
                //session.getInputStream().read(buf, 0, ContentLength);
            } catch(IOException e) {
                e.printStackTrace();
            }

            {
                for(int n = 0; n < buf.length; ++n) {
                    Log.d(TAG, String.format("buf[%2d] = %02X", n, buf[n]));
                }
            }

            postData = files.get("postData");
            Log.d(TAG, String.format("postData = %s ... %d", postData, postData.length()));

            byte[] postBytes = new byte[100];

            try {
                postBytes = postData.getBytes("UTF-8");
            } catch(UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            {
                bytesReturn[0] = (byte)0xA0;
                bytesReturn[1] = (byte)0x0A;
                bytesReturn[2] = (byte)0x00;
                bytesReturn[3] = (byte)0x30;
                bytesReturn[4] = (byte)0x00;
                bytesReturn[5] = (byte)0x00;
                bytesReturn[6] = (byte)0x00;
                bytesReturn[7] = (byte)0x00;
                bytesReturn[8] = (byte)0x00;
                bytesReturn[9] = (byte)0x00;

                lenReturn = 10;
            } 

            newFixedLengthResponse(Status.OK,"application/octet-stream",new ByteArrayInputStream(bytesReturn),lenReturn);
        } 
    }

    return newFixedLengthResponse(Status.OK,"application/octet-stream",new ByteArrayInputStream(bytesReturn),lenReturn);
}

如何正确发送字节数组而不丢失数据。

0 个答案:

没有答案