gpt4 book ai didi

c# - HttpWebRequest 始终显示相同的结果 C#

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:36 24 4
gpt4 key购买 nike

首先感谢您的帮助。我有一个让我抓狂的问题...

事实上,我在我创建的 Web 服务中发出了一个 GET 请求,并且一次运行良好。如果我之后执行相同的请求,则该请求不会到达我的网络服务。我收到的结果与第一个相同......这是一个错误,因为我在两个请求之间进行了一些更新。预期的结果是不同的。如果我在 Windows Phone 上重新启动应用程序,结果会更新。
在我的例子中,目标是获得我的联系人的位置。尽管我更新了他们的职位,但我收到了相同的职位。

        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);
});
}
}

最佳答案

这是一个缓存问题。在您的 Url 中添加 "&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();这会强制您的请求始终得到执行 - 这对我来说是个窍门。

关于c# - HttpWebRequest 始终显示相同的结果 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22857825/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com