问题描述
例如, API:http://127.0.0.1/api/alertstream 调用此URI后,将在设备和平台之间建立持久连接。 除非您自己关闭此API,否则不会关闭它。
Example:
--MIME_boundary
Content-Type: application/json; charset="UTF-8"
Content-Length: 650
{
Here is the json data.
}
--MIME_boundary
Content-disposition: form-data; name="Picture"; filename="Picture.jpeg"
Content-Type: image/jpeg
Content-Length: 82909
Here is the picture binary data.
.........more (Maybe json data . Maybe picture data.)
这是我的代码
但是它只能获取json数据进行分析,当API向我发送图像文件时,我想保存图像文件。
private Boolean alertStream_status = false;
private WebRequest request;
public void alertStream() //stream API
{
request = WebRequest.Create("http://127.0.0.1/api/alertstream");
request.Credentials = new NetworkCredential("admin","admin");
request.BeginGetResponse(ar =>
{
var req = (WebRequest)ar.AsyncState;
using (var response = req.EndGetResponse(ar))
{
int BYTES_TO_READ = 200000;
var buffer = new byte[BYTES_TO_READ];
using (Stream sm = response.GetResponseStream())
{
using (var reader = new StreamReader(sm))
{
string[] key = new string[] { "MIME_boundary","Content-Type","Content-Length","Content-disposition" };
// This loop goes as long as the api is streaming
int json_check = 0;
string json_obj = null;
while (!reader.EndOfStream)
{
if (alertStream_status)
{
var line = reader.ReadLine();
if (line == "{") json_check++;
if (json_check != 0) build_json(ref json_obj,line,ref json_check);
else
{
Boolean check_line = false;
for (int test_key = 0; test_key < 4; test_key++)
{
check_line = line.ToLower().Contains(key[test_key].ToLower());
if (check_line) break;
}
if (!check_line && line != null)
{
//else data (picture)
}
}
}
else
{
reader.dispose();
break;
}
}
}
}
}
},request);
}
private void build_json(ref string json,string line,ref int json_check)
{
if (line == "}") json_check--;
line = Regex.Replace(line,@"\s","");
if (json_check != 0) json = json + line;
else
{
json = json + line;
Console.WriteLine(json); //debug use
try
{
Json_Type_class.alertinfo content_body = JsonConvert.DeserializeObject<Json_Type_class.alertinfo>(json);
ProcessAlarm(content_body); //This place is a function to analyze json.
}
catch
{
}
json = null;
}
}
有人可以帮助我吗?非常感谢你 抱歉,我的英语不好,
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)