这是我的网络服务网址 http://www.newsquizapp.com/webservice/get_question
参数: 1. device_id 2. quiz_type
方法:发布
我已经检索了device_id& quiz_type 但我的问题是如何发送device_id& quiz_type,以便设备ID存储在Web服务中。
void SendPost()
{
var url = "http://www.newsquizapp.com/webservice/get_question";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
}
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest webRequest = (HttpWebRequest)callbackResult.AsyncState;
Stream postStream = webRequest.EndGetRequestStream(callbackResult);
// static device id
string postData = "device_id=001&quiz_type=all";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult callbackResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response;
response = (HttpWebResponse)webRequest.EndGetResponse(callbackResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
var Response = streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
// Error treatment
// ...
}
}
public void MainPage_Loaded(object sender, RoutedEventArgs e)
{
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(download);
client.DownloadStringAsync(new Uri("http://www.newsquizapp.com/webservice/get_question"));
}
public void download(object sender, DownloadStringCompletedEventArgs e)
{
var objMain = JsonConvert.DeserializeObject<Main>(e.Result);
question = objMain.questions;
DisplayQuestion();
}
protected void DisplayQuestion()
{
rb1.Visibility = Visibility.Visible;
rb2.Visibility = Visibility.Visible;
rb3.Visibility = Visibility.Visible;
rb4.Visibility = Visibility.Visible;
Question objQue = question[counter];
txtquestion.Text = objQue.questionTitle;
rb1.Content = objQue.answers[0].answer;
rb2.Content = objQue.answers[1].answer;
rb3.Content = objQue.answers[2].answer;
if (objQue.answers.Count == 3)
{
rb4.Visibility = Visibility.Collapsed;
}
else
{
rb4.Visibility = Visibility.Visible;
rb4.Content = objQue.answers[3].answer;
}
}
protected void radio_checked(object sender, RoutedEventArgs e)
{
btnFinish.Visibility = Visibility.Collapsed;
btnSubmit.Visibility = Visibility.Visible;
}
protected void btnsubmit_click(object sender, RoutedEventArgs e)
{
Question que = question[counter];
var correctAnswerId = que.correctAnswerId;
string answer = string.Empty;
if (que.answers[0].answerId == correctAnswerId)
{
answer = que.answers[0].answer;
}
if (que.answers[1].answerId == correctAnswerId)
{
answer = que.answers[1].answer;
}
if (que.answers[2].answerId == correctAnswerId)
{
answer = que.answers[2].answer;
}
if (que.answers.Count == 3)
{
rb4.Visibility = Visibility.Collapsed;
}
else if (que.answers[3].answerId == correctAnswerId)
{
answer = que.answers[3].answer;
}
if (rb1.IsChecked == true && que.answers[0].answerId == correctAnswerId)
{
txtanswer.Text = que.answers[0].answer;
rb1.Visibility = Visibility.Collapsed;
rb2.Visibility = Visibility.Collapsed;
rb3.Visibility = Visibility.Collapsed;
rb4.Visibility = Visibility.Collapsed;
}
else if (rb1.IsChecked == true && que.answers[0].answerId != correctAnswerId)
{
rb1.Visibility = Visibility.Visible;
rb2.Visibility = Visibility.Collapsed;
rb3.Visibility = Visibility.Collapsed;
rb4.Visibility = Visibility.Collapsed;
txtanswer.Text = answer;
}
else if (rb2.IsChecked == true && que.answers[1].answerId == correctAnswerId)
{
txtanswer.Text = que.answers[1].answer;
rb1.Visibility = Visibility.Collapsed;
rb2.Visibility = Visibility.Collapsed;
rb3.Visibility = Visibility.Collapsed;
rb4.Visibility = Visibility.Collapsed;
}
else if (rb2.IsChecked == true && que.answers[1].answerId != correctAnswerId)
{
rb1.Visibility = Visibility.Collapsed;
rb2.Visibility = Visibility.Visible;
rb3.Visibility = Visibility.Collapsed;
rb4.Visibility = Visibility.Collapsed;
txtanswer.Text = answer;
}
else if (rb3.IsChecked == true && que.answers[2].answerId == correctAnswerId)
{
txtanswer.Text = que.answers[2].answer;
rb1.Visibility = Visibility.Collapsed;
rb2.Visibility = Visibility.Collapsed;
rb3.Visibility = Visibility.Collapsed;
rb4.Visibility = Visibility.Collapsed;
}
else if (rb3.IsChecked == true && que.answers[2].answerId != correctAnswerId)
{
rb1.Visibility = Visibility.Collapsed;
rb2.Visibility = Visibility.Collapsed;
rb3.Visibility = Visibility.Visible;
rb4.Visibility = Visibility.Collapsed;
txtanswer.Text = answer;
}
else if (que.answers.Count == 3)
{
rb4.Visibility = Visibility.Collapsed;
}
else if (rb4.IsChecked == true && que.answers[3].answerId == correctAnswerId)
{
txtanswer.Text = que.answers[3].answer;
rb1.Visibility = Visibility.Collapsed;
rb2.Visibility = Visibility.Collapsed;
rb3.Visibility = Visibility.Collapsed;
rb4.Visibility = Visibility.Collapsed;
}
else if (rb4.IsChecked == true && que.answers[3].answerId != correctAnswerId)
{
rb1.Visibility = Visibility.Collapsed;
rb2.Visibility = Visibility.Collapsed;
rb3.Visibility = Visibility.Collapsed;
rb4.Visibility = Visibility.Visible;
txtanswer.Text = answer;
}
btnNext.Visibility = Visibility.Visible;
}
protected void btnnext_click(object sender, RoutedEventArgs e)
{
counter++;
if (counter >= question.Count)
{
txtanswer.Text = "no more question right now....";
}
else
{
DisplayQuestion();
}
}
}
一旦用户设备ID存储在数据库中,如果用户关闭应用程序并且他们将无法获得相同的问题。再打开它......
答案 0 :(得分:0)
也许你正在寻找HTTPRequest
。
http://dotnet-snippets.de/snippet/simple-web-request-with-web-response/215
答案 1 :(得分:0)
检查nemesisfixx的答案。 我发现它很有帮助。
如果您可以访问Web服务代码,则可以对其进行修改以将Device_uid作为url参数进行操作,从而避免所有额外的工作。我这样做了。
答案 2 :(得分:0)
如果您只想将数据发布到网络服务,可以使用 WebClient 类。这是实现您的目标的最简单方法。
示例:强>
Uri uri = new Uri("http://www.newsquizapp.com/webservice/get_question");
string data = "device_id=123&quiz_type=all";
WebClient wc = new WebClient();
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.UploadStringAsync(uri, data);
wc.UploadStringCompleted += wc_UploadComplete;
public void wc_UploadComplete(object sender, UploadStringCompletedEventArgs e)
{
string result = e.Result.ToString();
MessageBox.Show(result);
}
试一试。希望它有所帮助。