我正在尝试使用vs 2008从Windows CE项目中调用Web API,这是我的实现:
private void loginbtn_Click(object sender, EventArgs e)
{
username = usernametb.Text;
password = passwordtb.Text;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/WebSite1/CheckCredentials");
request.ContentType = "application/json";
// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";
request.SendChunked = true;
// start the asynchronous operation
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
// Keep the main thread from continuing while the asynchronous
// operation completes. A real world application
// could do something useful such as updating its user interface.
allDone.WaitOne();
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
var model = new login
{
username = "U0001",
password = "1212"
};
var json = JsonConvert.SerializeObject(model);
Console.WriteLine("Please enter the input data to be posted:");
string postData = json;
// Convert the string into a byte array.
byte[] byteArray = System.Text.Encoding.GetEncoding(1252).GetBytes(postData);
// Write to the request stream.
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
Console.WriteLine(responseString);
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
allDone.Set();
}
我收到此错误:“远程服务器返回错误:(501)未实现。” 在线: 远程服务器返回错误:(501)未实现。