我收到了错误
远程服务器返回错误:(403)Forbidden。
将一些xml数据发布到api时。下面是代码。 我已经从堆栈溢出中读到了一些其他问题,但是没有解决。
public static XmlDocument PlaceOrder(string Url, string Method, string payload)
{
Encoding enc = System.Text.Encoding.GetEncoding(1252);
HttpWebRequest apiCall = (HttpWebRequest)WebRequest.Create(Url);
byte[] byteArray = ASCIIEncoding.UTF8.GetBytes(payload);
apiCall.Accept = "*/*";
apiCall.KeepAlive = false;
apiCall.AllowAutoRedirect = false;
apiCall.Method = "POST";
apiCall.ContentType = "text/xml; encoding='utf-8'";
apiCall.ContentLength = byteArray.Length;
apiCall.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36";
apiCall.Headers.Add("Accept-Encoding", "gzip, deflate, br");
apiCall.Headers.Add("Accept-Language", "en-US,en;q=0.8");
apiCall.CookieContainer = new CookieContainer();
apiCall.UseDefaultCredentials = true;
apiCall.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
Stream dataStream = apiCall.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse webresponse;
webresponse = (HttpWebResponse)apiCall.GetResponse();
StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);
string response = loResponseStream.ReadToEnd();
XmlDocument toReturn = new XmlDocument();
toReturn.LoadXml(response);
return toReturn;
}