我是在C#中使用WebRequest的初学者,我正在尝试使用POST方法检查Apple在线商店(https://secure2.store.apple.com/order/list)中的订单。
我想检查一个订单,发现我应该使用带参数的POST方法:orderLookup-order-number(订单号)和orderLookup-order-user-info(我的Apple帐号)。
我试图通过以下方式将这两个参数发布到https://secure1.store.apple.com/us/shop/order/json/single上的https://www.hurl.it/:
发布到:https://secure1.store.apple.com/us/shop/order/json/single
parameter1:orderLookup-order-number value:[Order No]
parameter2: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;
}
当我想获得HttpWebResponse时出现503错误。谢谢。
答案 0 :(得分:0)
此解决方案正在运行。
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);
try
{
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();
}
htmlText = string.Empty;
}
catch (WebException e)
{
htmlText = string.Empty;
}
HttpWebRequest requestForStream = (HttpWebRequest)WebRequest.Create("https://secure1.store.apple.com/us/shop/order/json/single");
requestForStream.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0";
requestForStream.Method = "POST";
requestForStream.ContentType = "application/x-www-form-urlencoded";
byte[] postBytes = Encoding.ASCII.GetBytes(postData);
requestForStream.ContentLength = postBytes.Length;
using (Stream st = requestForStream.GetRequestStream())
{
st.Write(postBytes, 0, postBytes.Length);
}