0

我有一个进行 API 调用的 xamarin 表单应用程序,当 api 调用涉及非常小的 json 字符串时,它们可以完美地工作。一个有效的示例有效载荷是:

{"command":"","route":"check_password","password":"password","sqlctype":"","service":"",data:""}

一个不起作用的示例有效负载是:

{"command":"","route":"run_script","password":"","sqlctype":"","service":"Image_Processor",data:"base64 string that is 2MB in size"}

但是,对于第二个有效负载,streamWriter 无法成功写入并无限期挂起。我的代码在这里:

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"command\":\"\"," +
                              "\"route\":\""+route+"\","+
                              "\"password\":\""+password+"\"," +
                              "\"sqlctype\":\"\"," +
                              "\"service\":\""+service+"\"," +
                              "\"data\":\""+data+"\"}";

                streamWriter.Write(json);
            }

但有趣的是 API 操作在这两种情况下都成功完成

使用第一个有效负载运行的 cloudwatch 日志如下:

START RequestId: ee583521-f17d-4143-9c27-031ab781ddae Version: $LATEST
END RequestId: ee583521-f17d-4143-9c27-031ab781ddae
REPORT RequestId: ee583521-f17d-4143-9c27-031ab781ddae Duration: 7.01 ms Billed Duration: 8 ms Memory Size: 128 MB Max Memory Used: 93 MB XRAY TraceId: 1-611ac895-4ba33ffa6e181b6e66821bfc...

随着返回的身体

"T"

使用第二个有效负载运行的 cloudwatch 日志如下:

START RequestId: d78b16de-1a0b-4b35-877a-2fd6437dcd71 Version: $LATEST
END RequestId: d78b16de-1a0b-4b35-877a-2fd6437dcd71
REPORT RequestId: d78b16de-1a0b-4b35-877a-2fd6437dcd71 Duration: 4159.55 ms Billed Duration: 4160 ms Memory Size: 128 MB Max Memory Used: 93 MB XRAY TraceId: 1-611ac940-61cca9b762d1a2ff6...

随着返回的身体

"[{\"typ\": \"barcode\", \"outp\": \"0882658000546\", \"ymax\": \"0.6736111111111112\", \"ymin\": \"0.550843253968254\", \"xmax\": \"0.6736111111111112\", \"xmin\": \"0.3488756613756614\"}, {\"typ\": \"barcode\", \"outp\": \"A-0020-Z\", \"ymax\": \"0.4595734126984127\", \"ymin\": \"0.40153769841269843\", \"xmax\": \"0.6008597883597884\", \"xmin\": \"0.3194444444444444\"}, {\"typ\": \"barcode\", \"outp\": \"A-0010-Z\", \"ymax\": \"0.34077380952380953\", \"ymin\": \"0.29092261904761907\", \"xmax\": \"0.5929232804232805\", \"xmin\": \"0.328042328042328\"}]"

既然 API 显然在这两种情况下都在运行并成功运行,为什么应用程序会挂在

streamWriter.Write(json)

只有第二个有效载荷?

还需要注意的是,我也尝试执行此异步操作,并且无论有效负载如何,它都会挂在 client.postasync 上(两个有效负载都没有挂起)。代码:

        async Task<string> asyncAPI(string route, string password = "", string data = "", string service = "")
        {
            HttpClient client = new HttpClient();
            Uri uri = new Uri("https://uidvxrcio9.execute-api.us-east-2.amazonaws.com/default/Blueline_General_API");
            string json = JsonConvert.SerializeObject(new APIrequest(default, route, password, default, service, "dawdasdqwd2"));
            StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
            client.DefaultRequestHeaders.Add("x-api-key", "key");
            HttpResponseMessage response = null;
            response = await client.PostAsync(uri, content);
            return await response.Content.ReadAsStringAsync();
        }
4

1 回答 1

1

您为什么要尝试通过自己的代码进行序列化?使用 Newtonsoft.Json nuget 包并使用其中的方法。当您安装它们时,您可以轻松地序列化类的所有公共属性。序列化您的一个类的代码如下:

JsonConvert.SerializeObject(myCompany)

如果您尝试序列化原始数据....您可以在您的类中创建一个 byte[] 属性,例如:

public byte[] MyBytes {get; set;}
于 2021-08-13T20:13:21.730 回答