我是在C#中使用WebRequest的初學者,我嘗試使用POST方法在Apple Online Store(https://secure2.store.apple.com/order/list)中檢查我的訂單。HTTP POST方法失敗
我想檢查一個訂單,發現我應該使用POST方法和參數:orderLookup-order-number(Order No)and orderLookup-order-user-info(My Apple Account)。
我試圖通過對https://www.hurl.it/發佈這兩個參數來https://secure1.store.apple.com/us/shop/order/json/single:
POST TO:https://secure1.store.apple.com/us/shop/order/json/single
參數1:orderLookup階數的值:[訂單號]
參數2:orderLookup -order-user-info value:[Apple ID]
併成功獲得回報。但是,當我在C#中完成時,我失敗了。任何人都可以指出我的錯誤嗎?我的功能如下。
bool TryPostMethodToGetHtmlText(string orderNo, string accountNo, out string htmlText)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://secure1.store.apple.com/us/shop/order/json/single");
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0";
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
var postData = "orderLookup-order-number=" + orderNo;
postData += "&orderLookup-order-user-info=";
postData += HttpUtility.UrlEncode(accountNo);
byte[] postBytes = Encoding.ASCII.GetBytes(postData);
request.ContentLength = postBytes.Length;
using (Stream st = request.GetRequestStream())
{
st.Write(postBytes, 0, postBytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = null;
if (response.CharacterSet == null)
{
readStream = new StreamReader(receiveStream);
}
else
{
readStream = new StreamReader(receiveStream, Encoding.GetEncoding(65001));
}
htmlText = readStream.ReadToEnd();
response.Close();
readStream.Close();
return true;
}
htmlText = string.Empty;
return false;
}
我得到了一個503錯誤,當我想要得到的HttpWebResponse。謝謝。
503表示服務不可用響應。 – gypsyCoder
@gypsyCoder感謝您的回覆,但是當我未通過POST方法的任何參數時,我可以得到響應 – user
最佳猜測是身份驗證。 webrequest未經過身份驗證,因此服務器不允許您訪問數據。檢查如何進行身份驗證的API。 (在請求中請求+ auth cookie?auth頭?) –