Windows phone 8 c# httpclient第二次发送请求失败

本文关键字:请求 失败 第二次 httpclient phone Windows | 更新日期: 2023-09-27 17:53:08

我正在尝试一个简单的应用程序在Windows phone 8.1。我有一个按钮,并在第一次点击它发送一个请求,以插入数据通过url在php数据库。在第二点击它删除数据从同一表在数据库中通过另一个url在php。当我第一次运行它时。它从httpclient响应

向我发送以下响应
 StatusCode:200, ReasonPhase:'ok',   
 version:0.0,content:  
 System.Net.Http.streamContent,headers:  
   {  
    Date:thu,04 jun 2015 08:29:50 GMT  
    server:apache  
    server:phusion_passage/4.0.10  
        server: mod_bwlimited/1.4/5  
    server:mod_fcgid/2.3.9  
    X-powered-by:PHP/5.4/5.4.36  
    Keep-Alive:timeout=3,max=30  
    connection:keep-alive  
    Transfer-encoding:chunked  
    content-Type:text/html  
   }

和第二次单击时,我也得到了相同的删除响应。如果我再次按下按钮,我如何得到以下msg

StatusCode:200, ReasonPhase:'ok',
version:0.0,content:
System.Net.Http.streamContent,headers:
{
    X-powered-by:PHP/5.4/5.4.36
    Keep-Alive:timeout=3,max=30
    Transfer-encoding:chunked
    content-Type:text/html
}

,显然没有数据插入到数据库中。

我在紧急启动它,因为这是一个大项目的一小部分。我的代码如下:

声明:

 url = "http://demo.in/customer_info.php?";
            url_delete = "http://demo.in/delete_noti.php?device_id=";
data1 = url + "device_id=4000";
data_delete = url_delete + "4000";

方法:

   private async void sendData(string data)
    {
       // MessageBox.Show(data1);
        using (HttpClient hc = new HttpClient())
        {
            var response = await hc.GetAsync(data);
            MessageBox.Show(response.ToString());
            hc.Dispose();
        }
    }
    private async void deleteData(string data)
    {
        //MessageBox.Show(url_delete1);
        using (HttpClient hc = new HttpClient())
        {
            var response = await hc.GetAsync(data);
            //MessageBox.Show(hc1.ToString());
            MessageBox.Show(response.ToString());
            hc.Dispose();
        }
    }

用法:

private void btn_click(object sender, RoutedEventArgs e)
    {
        if (count == 0)
        {
            sendData(data1);
            (sender as Button).Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(225, 247, 98, 15));
            MessageBox.Show("sending");
            //    suspend(a);
            count = 1;
        }
        else
        {
            deleteData(url_delete);
            (sender as Button).Background = new SolidColorBrush(Colors.Black);
            MessageBox.Show("deleting");
            count = 0;
        }
    }

Windows phone 8 c# httpclient第二次发送请求失败

正如@Null指针所建议的,这可能是缓存问题。

你可以像他说的那样给url添加一个随机参数,或者如果你不或不能给url添加参数,你可以添加这行代码来忽略缓存:

hc.DefaultRequestHeaders.IfModifiedSince = DateTime.Now;