有些時候簡單的事情可能會讓你失望,這裏是我的一個故事。request.Method =「POST」正在工作,但不是request.ContentLength
我想做一個簡單的網絡請求來驗證用戶名和密碼。它在Windows Phone 8中工作得很好,但我似乎無法得到相同的代碼在Windows 8上工作。
我明白我無法做一個GetResponse,就像我使用Windows Phone做的那樣,所以我使用GetResponseAsync和那部分如果工作正常。但服務器的響應是它沒有在頭中獲得「POST」組件。
這裏是Windows Phone 8的代碼,正在我的手機版本精細
private async void VerifyUser()
{
//System.Diagnostics.Debug.WriteLine("aa");
loginParams = "username=" + username + "&password=" + password;
string teamResponse = "https://mysite.com/mystuff/LoginApp?" + loginParams;
var request = HttpWebRequest.Create(teamResponse) as HttpWebRequest;
request.Method = "POST";
request.Accept = "application/json;odata=verbose";
var factory = new TaskFactory();
var task = factory.FromAsync<WebResponse>(request.BeginGetResponse, request.EndGetResponse, null);
//System.Diagnostics.Debug.WriteLine("bb");
try
{
var response = await task;
System.IO.Stream responseStream = response.GetResponseStream();
string data;
using (var reader = new System.IO.StreamReader(responseStream))
{
data = reader.ReadToEnd();
}
responseStream.Close();
//System.Diagnostics.Debug.WriteLine("cc");
webData = data;
//MessageBox.Show(data);
}
catch (Exception e)
{
MessageBox.Show("There was a network error. Please check your network connectivty and try again " + e);
}
// System.Diagnostics.Debug.WriteLine(webData);
JToken token = JObject.Parse(webData);
string success = (string)token.SelectToken("success");
以下是我對Windows 8的使用Visual Studio 2013
private async void VerifyUser()
{
string data;
loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
string teamResponse = "https://mysite.com/mystuff/LoginApp?" + loginParams;
Debug.WriteLine(teamResponse);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(teamResponse);
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
using (var sr = new StreamReader(response.GetResponseStream()))
{
data = sr.ReadToEnd();
}
Debug.WriteLine(data);
}
這一工程,但我得到返回一個簡單的迴應,只提示用戶已登錄或未登錄。服務器小組說,該請求在標題中沒有「POST」。
所以我添加以下代碼:
request.Method = "POST";
request.Accept = "application/json;odata=verbose";
這裏是全碼:
private async void VerifyUser()
{
string data;
loginParams = "username=" + logInUserIdString + "&password=" + logInPasswordString;
string teamResponse = "https://mysite.com/mystuff/LoginApp?" + loginParams;
Debug.WriteLine(teamResponse);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(teamResponse);
request.Method = "POST";
request.Accept = "application/json;odata=verbose";
HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
using (var sr = new StreamReader(response.GetResponseStream()))
{
data = sr.ReadToEnd();
}
Debug.WriteLine(data);
}
然後它就引發以下:
'web1.exe' (CLR v4.0.30319: Immersive Application Domain): Loaded 'C:\Windows\system32\WinMetadata\Windows.Foundation.winmd'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.Net.WebException' occurred in mscorlib.dll
An exception of type 'System.Net.WebException' occurred in mscorlib.dll but was not handled in user code
Additional information: The remote server returned an error: (411) Length Required.
那麼,爲什麼doco說我可以在頭文件中加入「POST」嗎?任何幫助將非常感激。
更新:我現在知道它與loginParams的長度有關的問題。在我的IOS,Android和WindowsPhone應用程序中,我不必指定長度並且效果很好,但Visual Studio 2013 Windows應用程序不接受由於某種原因而設置內容長度。
以下是錯誤: System.Net.HttpWebRequest不包含的ContentLength的定義,並沒有擴展方法的ContentLength接受類型System.Net.HttpWebRequest的拳頭論點可以找到(是否缺少using指令或一個程序集參考?)
那麼爲什麼我不能添加request.ContentLength?
IN對Domniks請求代碼的響應我附上了帶有錯誤消息的代碼塊圖像。再次感謝您的幫助。 我重視我的屏幕,它不會ACC
感謝您的回覆。 GET給了我同樣的結果,就好像我沒有包含請求一樣。方法。 Web響應給出了以下輸出:{「logged_in」:false,「success」:null,「message」:null,「full_login」:null}成功應返回true或false非空 – timv