How to solve The remote server returned an error: (400) Bad Request when ContentLength increases
How to solve The remote server returned an error: (400) Bad Request when ContentLength increases
每当我尝试向超过 20 个用户发送通知时,它都会出现错误远程服务器返回错误:(400) 错误请求。我已经尝试了所有的解决方案,但它给出了同样的错误。
这是我的代码:-
private string SendGCMNotification(string apiKey, string postData)
{
try
{
WebRequest wRequest;
wRequest = WebRequest.Create("https://gcm-http.googleapis.com/gcm/send");
wRequest.Method = "POST";
wRequest.ContentType = "application/json;charset=UTF-8";
wRequest.Headers.Add(string.Format("Authorization: key={0}", apiKey));
var bytes = Encoding.UTF8.GetBytes(postData);
wRequest.ContentLength = bytes.Length;
var stream = wRequest.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
var wResponse = wRequest.GetResponse();
stream = wResponse.GetResponseStream();
var reader = new StreamReader(stream);
var response = reader.ReadToEnd();
var httpResponse = (HttpWebResponse)wResponse;
var status = httpResponse.StatusCode.ToString();
reader.Close();
stream.Close();
wResponse.Close();
//TODO check status
}
catch (WebException ex)
{
Response.Write(ex.Message.ToString());
}
return "sent";
}
我的 web.config 文件 :-
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="30000000" maxUrl="10000" maxQueryString="10000" />
</requestFiltering>
</security>
</system.webServer>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000" />
</webServices>
</scripting>
</system.web.extensions>
问题出在我的 postdata 中。<div>
标记被插入到带有端点的数据库中。
这是导致问题的代码
{ "registration_ids": [ "e-9zZTY0LtI:APA91bFTZUYD4kghBu1xU4cO3A9aqzCN_QqX7fZECoQlcGC-PvDg5_fOPMZgNHTBZqk-4jwk6iAfWjJlD6BUu4iGmnXHqjpo-40FR6f4kgjnHGS6TBGOR2Vii7CqxPhvr7OE1RyX8JHy <div style="display:none">viagra.com coupon</div>" ], "data": {"contentTitle":"content title GCM", "message": "some test message"}}
通过从注册 ID 中删除它解决了我的问题
<div style="display:none">viagra.com coupon</div>
每当我尝试向超过 20 个用户发送通知时,它都会出现错误远程服务器返回错误:(400) 错误请求。我已经尝试了所有的解决方案,但它给出了同样的错误。 这是我的代码:-
private string SendGCMNotification(string apiKey, string postData)
{
try
{
WebRequest wRequest;
wRequest = WebRequest.Create("https://gcm-http.googleapis.com/gcm/send");
wRequest.Method = "POST";
wRequest.ContentType = "application/json;charset=UTF-8";
wRequest.Headers.Add(string.Format("Authorization: key={0}", apiKey));
var bytes = Encoding.UTF8.GetBytes(postData);
wRequest.ContentLength = bytes.Length;
var stream = wRequest.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
var wResponse = wRequest.GetResponse();
stream = wResponse.GetResponseStream();
var reader = new StreamReader(stream);
var response = reader.ReadToEnd();
var httpResponse = (HttpWebResponse)wResponse;
var status = httpResponse.StatusCode.ToString();
reader.Close();
stream.Close();
wResponse.Close();
//TODO check status
}
catch (WebException ex)
{
Response.Write(ex.Message.ToString());
}
return "sent";
}
我的 web.config 文件 :-
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="30000000" maxUrl="10000" maxQueryString="10000" />
</requestFiltering>
</security>
</system.webServer>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000" />
</webServices>
</scripting>
</system.web.extensions>
问题出在我的 postdata 中。<div>
标记被插入到带有端点的数据库中。
这是导致问题的代码
{ "registration_ids": [ "e-9zZTY0LtI:APA91bFTZUYD4kghBu1xU4cO3A9aqzCN_QqX7fZECoQlcGC-PvDg5_fOPMZgNHTBZqk-4jwk6iAfWjJlD6BUu4iGmnXHqjpo-40FR6f4kgjnHGS6TBGOR2Vii7CqxPhvr7OE1RyX8JHy <div style="display:none">viagra.com coupon</div>" ], "data": {"contentTitle":"content title GCM", "message": "some test message"}}
通过从注册 ID 中删除它解决了我的问题
<div style="display:none">viagra.com coupon</div>