HttpWebRequest总是显示相同的结果C#

时间:2014-04-04 08:48:12

标签: c# web-services get windows-phone httpwebrequest

首先感谢你的帮助。我有一个让我发疯的问题......

实际上,我在我的Web服务中发出了一个GET请求,并且它工作正常。 如果我之后执行了相同的请求,则请求无法访问我的网络服务。我收到的结果与第一个结果相同......这是一个错误,因为我在两个请求之间做了一些更新。预期的结果是不同的。如果我在Windows手机上重新启动应用程序,结果会更新。
在我看来,我的目标是获得联系人的立场。尽管我更新了他们的职位,我仍然获得相同的职位。

        private void Button_Click(object sender, RoutedEventArgs e)
    {
        System.Uri targetUri = new System.Uri("http://My-Ip-Address:8080/WhereAreMyFriends/webresources/positions/getMyContactsPositions/0660066000");
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
        request.Method = "GET";
        request.Accept = "application/xml";
        request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
    }

    private void ReadWebRequestCallback(IAsyncResult callbackResult)
    {
        try
        {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);

            if (myResponse.StatusCode == HttpStatusCode.OK)
            {
                using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
                {
                    string results = httpwebStreamReader.ReadToEnd();
                    Dispatcher.BeginInvoke(() =>
                    {
                        MessageBox.Show(results);//NavigationService.Navigate(new Uri("/CodeVerification.xaml", UriKind.Relative));
                    });
                }
                myResponse.Close();
            }
            else
            {
                Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show("Status Code incorrect");
                    myResponse.Close();
                });
            }
        }
        catch (Exception e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show(e.Message);
            });
        }
    }

1 个答案:

答案 0 :(得分:1)

这是一个缓存问题。添加"& random =" + DateTime.Now.Ticks"您的网址中的代码可以解决您的问题。

private void Button_Click(object sender, RoutedEventArgs e)
     {
        System.Uri targetUri = new System.Uri("http://My-Ip-Address:8080/WhereAreMyFriends/webresources/positions/getMyContactsPositions/0660066000"+"&random="+DateTime.Now.Ticks);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
        request.Method = "GET";
        request.Accept = "application/xml";
        request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
     }

另一种方法是

private void Button_Click(object sender, RoutedEventArgs e)
         {
            System.Uri targetUri = new System.Uri("http://My-Ip-Address:8080/WhereAreMyFriends/webresources/positions/getMyContactsPositions/0660066000");
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
            request.Headers[HttpRequestHeader.CacheControl] = "no-cache"; 
            request.Headers[HttpRequestHeader.Pragma] = "no-cache"; 
            request.Headers[HttpRequestHeader.IfModifiedSince] = dateTime.Now.ToString();
            request.Method = "GET";
            request.Accept = "application/xml";
            request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
         }

request.Headers [HttpRequestHeader.IfModifiedSince] = dateTime.Now.ToString(); 这迫使你的请求总是被执行 - 这对我来说是个窍门。