我可以成功登錄,但登錄後似乎無法成功調用其他方法。我在網上搜索我需要保持HTTP會話之間的通話,但我已經做到了,仍然無法正常工作。你可以看看下面的示例代碼,看看還有什麼缺失?謝謝。登錄後維護HTTP會話
端點:HTTP:// 「我的域」/帳號/登錄
方法:GET
參數:用戶名,密碼
我似乎無法登錄後請求的URL下,當成功調用成功上面網址:
端點:HTTP:// 「我的域名」/原料藥/遊戲
Parmaters:無
public string MakeRequest(string parameters)
{
CookieContainer cookie = new CookieContainer();
string URL = EndPoint + parameters;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.CookieContainer = cookie;
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = ContentType;
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
{
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
你是什麼意思「HTTP會話」? – mason
只是試圖在客戶端上通過HTTP維護會話。 – Phillip
網絡是無狀態的,所以我不知道「通過HTTP維護會話」是什麼意思。 – mason