我试图解决这个问题,但遗憾的是没有什么对我有帮助。我在下面列出了带有***错误点的代码。我需要帮助,请告诉我代码有什么问题。
代码:
public MainViewModel()
{
System.Uri myUri = new System.Uri("http://192.168.0.103/new_ringtone");
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/json;charset=utf-8";
myRequest.Headers["user-agent"] = "RingtoneApplication";
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
// End the stream request operation
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
// Create the post data
string currentDateTime = DateTime.Now.ToShortDateString();
string deviceName = Microsoft.Phone.Info.DeviceStatus.DeviceName;
string osVersion =Environment.OSVersion.Version.ToString();
Dictionary<string, object> userInfoDictionary = new Dictionary<string, object>();
userInfoDictionary.Add("DeviceName", deviceName);
userInfoDictionary.Add("OSVersion", osVersion);
userInfoDictionary.Add("DateTime", currentDateTime);
Dictionary<string, object> user = new Dictionary<string, object>();
user.Add("User", userInfoDictionary);
Dictionary<string, string> ringtoneappDictionary = new Dictionary<string, string>();
ringtoneappDictionary.Add("Ringtoneapp", "category_id");
Dictionary<string, string> userAgent = new Dictionary<string, string>();
userAgent.Add("UserAgent", "RingtoneApplication");
Object[] dataArray = new object[2];
dataArray[0] = user;
dataArray[1] = ringtoneappDictionary;
//dataArray[2] = userAgent;
Dictionary<string, Array> data = new Dictionary<string, Array>();
data.Add("Data", dataArray);
string jsonData = JsonConvert.SerializeObject(data);
var token = CreateToken(jsonData, GlobalClass.SecretKey);
string postData = "method=getRintoneCategories&data="+jsonData+"&Token="+token;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
}
void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
***HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);***
using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
{
string result = httpWebStreamReader.ReadToEnd();
//For debug: show results
Debug.WriteLine(result);
}
}
例外:
{System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at Ringtone.ViewModel.MainViewModel.GetResponsetStreamCallback(IAsyncResult callbackResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass1d.<InvokeGetResponseCallback>b__1b(Object state2)}