验证PayPal IPN接收数据时出现414请求URI过长错误排查
PayPal IPN验证414错误与代码修复
问题根源
你代码里的核心错误是:把POST请求的参数直接拼接到了URL的查询字符串中,即便设置了Method = WebRequestMethods.Http.Post,但实际没有将参数写入POST请求的请求体,而是让参数挂在URL后面。这会导致两个问题:
- URL长度超过限制触发414「Request-URI Too Large」错误
- PayPal端识别到URL里的参数,判定这是GET请求而非POST,和你反馈的PayPal回复一致
修正后的代码
Try 'gather the info Dim strContent As String = String.Empty Using reader = New StreamReader(Request.InputStream) strContent = reader.ReadToEnd() End Using mstrActualContent = strContent ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 'create webrequest - 仅使用基础验证URL,不再拼接参数 Dim hwrIPNVerify As HttpWebRequest = DirectCast(WebRequest.Create(HelperClasses.PayPal.IPNVerifyUrl), HttpWebRequest) hwrIPNVerify.UserAgent = "ASP-IPN-ipnlistener" hwrIPNVerify.Method = WebRequestMethods.Http.Post hwrIPNVerify.ContentType = "application/x-www-form-urlencoded" '将参数写入POST请求体(关键修改) Dim postData As String = mcVerifyIPNPrefix & strContent Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postData) hwrIPNVerify.ContentLength = byteArray.Length Using dataStream As Stream = hwrIPNVerify.GetRequestStream() dataStream.Write(byteArray, 0, byteArray.Length) End Using '获取响应 Dim hwrIPNVerifyResp As HttpWebResponse = DirectCast(hwrIPNVerify.GetResponse(), HttpWebResponse) Using oStreamReader = New StreamReader(hwrIPNVerifyResp.GetResponseStream()) mstrResponse = oStreamReader.ReadToEnd() End Using If mstrResponse = "VERIFIED" Then 'process data Else 'log error End If Catch ex As Exception Throw End Try
修改要点说明
- 移除URL中的参数拼接,仅使用纯净的
IPNVerifyUrl创建请求 - 将待提交的参数(
mcVerifyIPNPrefix & strContent)转换为字节数组,通过GetRequestStream()写入POST请求体 - 补充设置
ContentLength,确保请求头和请求体长度一致,符合HTTP规范
这样修改后,参数会通过POST请求体传递,不会占用URL长度,PayPal也会正确识别为POST请求,解决414错误和请求方法误判的问题。
内容的提问来源于stack exchange,提问作者sd4ksb
相关产品推荐
相关产品推荐

