所以我正在关注 WingTip Toy tutorial ,我知道它有点旧,但它没有错误,直到我需要使用沙盒开发工具使用 PayPal 结帐
这是发生错误的代码
//Retrieve the Response returned from the NVP API call to PayPal.
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
string result;
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
}
return result;
这是我运行时遇到的错误
[ProtocolViolationException: You must write ContentLength bytes to the request stream before calling [Begin]GetResponse.]
请注意我是初学者
编辑:完整代码在这里
public string HttpCall(string NvpRequest)
{
string url = pEndPointURL;
string strPost = NvpRequest + "&" + buildCredentialsNVPString();
strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Timeout = Timeout;
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
try
{
using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
{
myWriter.Write(strPost);
}
}
catch (Exception)
{
// No logging for this tutorial.
}
//Retrieve the Response returned from the NVP API call to PayPal.
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
string result;
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
}
return result;
}
最佳答案
objRequest.ContentLength = strPost.Length;
你想在这里做什么?框架自动设置内容长度。内容长度以字节为单位,但您已给出字符数。
这就是错误提示的原因:你写的字符数与你说的不同。
删除该行。
如果您使用HttpClient
,您的代码会变得简单得多。应该大约 5 行。
关于c# - 尝试结帐时 PayPal 功能中的 GetResponse() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37430752/