简体   繁体   English

Windows Phone 8 C#httpclient剂量第二次发送请求

[英]windows phone 8 c# httpclient dosent send request for second time

i am trying for a simple app in windows phone 8.1. 我正在尝试在Windows Phone 8.1中使用一个简单的应用程序。 i have a button and on first time click it send a request to insert data into the database via url in php. 我有一个按钮,第一次单击它会发送一个请求,要求通过php中的url将数据插入数据库。 on second click it delete the data from the same table in database via another url in php. 第二次单击它通过php中的另一个URL从数据库的同一表中删除数据。 when i first run it. 当我第一次运行它。 it send me the following response from httpclient response 它从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  
   }

and on second click i also get the same response for deleting. 在第二次点击我也得到相同的删除响应。 if i again press the button i some how get the following 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
}

and obviously no data is inserted into the database. 并且显然没有数据插入到数据库中。

i am in emergency to launch it as this is a small part of a big project. 我急于启动它,因为这只是一个大项目的一小部分。 my code is as follows: 我的代码如下:

declaration: 宣言:

 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";

method: 方法:

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

usage: 用法:

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

As @Null Pointer suggested, it might be a caching issue. 正如@Null Pointer所建议的,这可能是一个缓存问题。

You can add a random parameter to the url as he said or you can add this line of code to ignore cache if you don't or can't add parameter to the url: 您可以按照他所说的向URL添加一个随机参数,或者如果您没有或不能向URL添加参数,则可以添加以下代码以忽略缓存:

hc.DefaultRequestHeaders.IfModifiedSince = DateTime.Now;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM