[英]How to write to Stream object returned from WebRequest.GetRequestStream()
I am trying to send FCM push alarm to an Android app using HTTP request in C#.我正在尝试使用 C# 中的 HTTP 请求将 FCM 推送警报发送到 Android 应用程序。 I've seen some examples and wrote them as a reference, but I ran into problems where I didn't expect them.我看过一些例子并将它们写下来作为参考,但我遇到了我没想到的问题。
I got a data stream returned through an HTTP request, and I wrote data to it with the Write() method, but an exception occurred.得到一个通过HTTP请求返回的数据流,我用Write()方法向其写入数据,但是发生了异常。
Here is my code.这是我的代码。
public void PushNotification()
{
const string FCM_URL = "https://fcm.googleapis.com/v1/projects/xxx/messages:send HTTP/1.1";
const string SERVER_KEY = "xxxxxx";
const string SENDER_ID = "xxxxx";
string postbody;
Byte[] byteArr;
object payload;
WebRequest wReq;
wReq = WebRequest.Create(FCM_URL);
wReq.Method = "post";
wReq.Headers.Add(string.Format("Autorization:key={0}", SERVER_KEY));
wReq.Headers.Add(string.Format("Sender:id={0}", SENDER_ID));
wReq.ContentType = "application/json";
payload = new
{
to = "/topics/all",
priority = "high",
content_available = true,
notification = new
{
body = "Test",
title = "Test",
badge = 1
},
data = new
{
key1 = "val1",
key2 = "val2"
}
};
postbody = JsonConvert.SerializeObject(payload).ToString();
byteArr = Encoding.UTF8.GetBytes(postbody);
wReq.ContentLength = byteArr.Length;
using (Stream dStream = wReq.GetRequestStream())
{
dStream.Write(byteArr, 0, byteArr.Length);
using (WebResponse wResp = wReq.GetResponse()) //Exception: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.
{
using (Stream dStreamResp = wResp.GetResponseStream())
{
if (dStreamResp != null) using (StreamReader wReader = new StreamReader(dStreamResp))
{
string sRespFromServer = wReader.ReadToEnd();
}
}
}
}
}
How I solve it???我怎么解决???
I think I made a mistake because I am not familiar with C#.我想我犯了一个错误,因为我不熟悉 C#。 Since the 'using' keyword was used, the stream was not closed after writing.由于使用了“using”关键字,因此写入后流并未关闭。 So I modified the code to:于是我将代码修改为:
public void PushData(string messageTitle, string messageText)
{
string postbody;
object payload;
Byte[] byteArr;
WebRequest wReq;
WebResponse wResp;
Stream dataStream, dataRespStream;
wReq = WebRequest.Create(FCM_ENDPOINT);
wReq.Method = "POST";
wReq.Headers.Add(string.Format("Authorization: key={0}", serverApiKey));
wReq.Headers.Add(string.Format("Sender: id={0}", senderID));
wReq.ContentType = "application/json";
payload = new
{
to = "/topics/" + _receiverGrpID,
priority = "high",
content_available = true,
data = new
{
title = messageTitle,
body = messageText
}
};
postbody = JsonConvert.SerializeObject(payload).ToString();
byteArr = Encoding.UTF8.GetBytes(postbody);
wReq.ContentLength = byteArr.Length;
dataStream = wReq.GetRequestStream();
dataStream.Write(byteArr, 0, byteArr.Length);
dataStream.Close();
wResp = wReq.GetResponse();
dataRespStream = wResp.GetResponseStream();
if(dataRespStream != null)
{
StreamReader streamReader = new StreamReader(dataRespStream);
_respFromFCMServer = streamReader.ReadToEnd();
}
dataRespStream.Close();
}
Even I was wrong with the request URL.甚至我的请求 URL 也错了。 It is natural that this doesn't work.这是行不通的,这是很自然的。
After solving these, this works!解决这些问题后,这有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.