예제 #1
0
        public void testGetUrl()
        {
            HttpClient client = HttpClient.Init("http://www.abc.com/xy.aspx", "GET");

            Assert.AreEqual("http://www.abc.com/xy.aspx", client.GetRequestUrl());

            // 在 GET 情况下,param 和 query string 都会拼接

            client = HttpClient.Init("http://www.abc.com/xy.aspx", "GET");
            client.AddParam("name", "zhangsan");
            client.AddParam("gender", 1);
            client.AddParam("day", "2012-12-12");
            Assert.AreEqual("http://www.abc.com/xy.aspx?name=zhangsan&gender=1&day=2012-12-12", client.GetRequestUrl());

            client = HttpClient.Init("http://www.abc.com/xy.aspx", "GET");
            client.AddQuery("name", "zhangsan");
            client.AddQuery("gender", 1);
            client.AddQuery("day", "2012-12-12");
            Assert.AreEqual("http://www.abc.com/xy.aspx?name=zhangsan&gender=1&day=2012-12-12", client.GetRequestUrl());

            client = HttpClient.Init("http://www.abc.com/xy.aspx", "GET");
            client.AddParam("name", "zhangsan");
            client.AddQuery("gender", 1);
            client.AddParam("day", "2012-12-12");
            client.AddQuery("location", "beijing");
            // 先拼接 query,然后拼接 params
            Assert.AreEqual("http://www.abc.com/xy.aspx?gender=1&location=beijing&name=zhangsan&day=2012-12-12", client.GetRequestUrl());

            client = HttpClient.Init("http://www.abc.com/xy.aspx?z1=v1&z2=v2", "GET");
            client.AddQuery("name", "zhangsan");
            client.AddQuery("gender", 1);
            client.AddQuery("day", "2012-12-12");
            Assert.AreEqual("http://www.abc.com/xy.aspx?z1=v1&z2=v2&name=zhangsan&gender=1&day=2012-12-12", client.GetRequestUrl());
        }
예제 #2
0
        public void testPostUrl()
        {
            HttpClient client = HttpClient.Init("http://www.abc.com/xy.aspx", "POST");

            Assert.AreEqual("http://www.abc.com/xy.aspx", client.GetRequestUrl());


            // 在 POST 情况下,仅拼接 query string

            client = HttpClient.Init("http://www.abc.com/xy.aspx", "POST");
            client.AddParam("name", "zhangsan");
            client.AddParam("gender", 1);
            client.AddParam("day", "2012-12-12");
            Assert.AreEqual("http://www.abc.com/xy.aspx", client.GetRequestUrl());

            client = HttpClient.Init("http://www.abc.com/xy.aspx", "POST");
            client.AddQuery("name", "zhangsan");
            client.AddQuery("gender", 1);
            client.AddQuery("day", "2012-12-12");
            Assert.AreEqual("http://www.abc.com/xy.aspx?name=zhangsan&gender=1&day=2012-12-12", client.GetRequestUrl());

            client = HttpClient.Init("http://www.abc.com/xy.aspx", "POST");
            client.AddParam("name", "zhangsan");
            client.AddQuery("gender", 1);
            client.AddParam("day", "2012-12-12");
            client.AddQuery("location", "beijing");
            Assert.AreEqual("http://www.abc.com/xy.aspx?gender=1&location=beijing", client.GetRequestUrl());

            // 如果不指定 HttpMethod,则使用 GET
            client = HttpClient.Init("http://www.abc.com/xy.aspx", null);
            client.AddParam("name", "zhangsan");
            client.AddQuery("gender", 1);
            client.AddParam("day", "2012-12-12");
            client.AddQuery("location", "beijing");
            Assert.AreEqual("http://www.abc.com/xy.aspx?gender=1&location=beijing&name=zhangsan&day=2012-12-12", client.GetRequestUrl());

            client = HttpClient.Init("http://www.abc.com/xy.aspx?z1=v1&z2=v2", "POST");
            client.AddParam("name", "zhangsan");
            client.AddQuery("gender", 1);
            client.AddParam("day", "2012-12-12");
            client.AddQuery("location", "beijing");
            Assert.AreEqual("http://www.abc.com/xy.aspx?z1=v1&z2=v2&gender=1&location=beijing", client.GetRequestUrl());
        }
예제 #3
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Bt_SendData_Click(object sender, RoutedEventArgs e)
        {
            Uri serverUri = null;

            try
            {
                serverUri = new Uri(this.tb_SendUrl.Text);
            }
            catch
            {
                MessageBox.Show("目标主机不正确,无法发送数据!", "错误", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                return;
            }

            if (comboBoxPostGetChoice.Text == "POST")
            {
                httpClient.MethodInit(Methods.POST);

                //数据
                if (checkBox_PostData.IsChecked == true)
                {
                    DockPanel dockPanel = groupBox_PostData.Content as DockPanel;
                    if (dockPanel == null)
                    {
                        return;
                    }

                    StackPanel stackPanel = dockPanel.Children[1] as StackPanel;
                    if (stackPanel == null)
                    {
                        return;
                    }

                    foreach (var item in stackPanel.Children)
                    {
                        DockPanel dockPanel_1 = item as DockPanel;
                        if (dockPanel_1 == null)
                        {
                            continue;
                        }

                        StackPanel stackPanel_11 = dockPanel_1.Children[0] as StackPanel;
                        if (stackPanel_11 == null)
                        {
                            continue;
                        }


                        StackPanel stackPanel_12 = dockPanel_1.Children[2] as StackPanel;
                        if (stackPanel_12 == null)
                        {
                            continue;
                        }


                        RadioButton radioButton = stackPanel_11.Children[0] as RadioButton;
                        if (radioButton == null)
                        {
                            continue;
                        }

                        // Content="字符串"
                        if (radioButton.IsChecked == true)
                        {
                            TextBox textBox = stackPanel_12.Children[0] as TextBox;
                            if (textBox == null)
                            {
                                continue;
                            }
                            httpClient.AddString(textBox.Text);
                        }
                        else
                        {
                            TextBox textBox = stackPanel_12.Children[1] as TextBox;
                            if (textBox == null)
                            {
                                continue;
                            }
                            httpClient.AddHex(textBox.Text);
                        }
                    }
                }
                //字段
                if (checkBox_PostParam.IsChecked == true)
                {
                    DockPanel dockPanel = groupBox_PostParam.Content as DockPanel;
                    if (dockPanel == null)
                    {
                        return;
                    }

                    StackPanel stackPanel = dockPanel.Children[1] as StackPanel;
                    if (stackPanel == null)
                    {
                        return;
                    }

                    foreach (var item in stackPanel.Children)
                    {
                        DockPanel dockPanel_1 = item as DockPanel;
                        if (dockPanel_1 == null)
                        {
                            continue;
                        }


                        TextBox textBox0 = dockPanel_1.Children[0] as TextBox;
                        if (textBox0 == null)
                        {
                            continue;
                        }

                        TextBox textBox3 = dockPanel_1.Children[3] as TextBox;

                        httpClient.AddParam(textBox0.Text, textBox3.Text);
                    }
                }
                //文件
                if (checkBox_PostFile.IsChecked == true)
                {
                    DockPanel dockPanel = groupBox_PostFile.Content as DockPanel;
                    if (dockPanel == null)
                    {
                        return;
                    }

                    StackPanel stackPanel = dockPanel.Children[1] as StackPanel;
                    if (stackPanel == null)
                    {
                        return;
                    }

                    foreach (var item in stackPanel.Children)
                    {
                        DockPanel dockPanel_1 = item as DockPanel;
                        if (dockPanel_1 == null)
                        {
                            continue;
                        }

                        TextBox textBox2 = dockPanel_1.Children[2] as TextBox;
                        if (textBox2 == null)
                        {
                            continue;
                        }

                        httpClient.AddFile(textBox2.Text);
                    }
                }
            }
            else
            {
                httpClient.MethodInit(Methods.GET);

                //数据
                if (checkBox_GetData.IsChecked == true)
                {
                    DockPanel dockPanel = groupBox_GetData.Content as DockPanel;
                    if (dockPanel == null)
                    {
                        return;
                    }

                    StackPanel stackPanel = dockPanel.Children[1] as StackPanel;
                    if (stackPanel == null)
                    {
                        return;
                    }

                    foreach (var item in stackPanel.Children)
                    {
                        DockPanel dockPanel_1 = item as DockPanel;
                        if (dockPanel_1 == null)
                        {
                            continue;
                        }

                        TextBox textBox = dockPanel_1.Children[2] as TextBox;
                        if (textBox == null)
                        {
                            continue;
                        }

                        httpClient.AddString(textBox.Text);
                    }
                }
                //字段
                if (checkBox_GetParam.IsChecked == true)
                {
                    DockPanel dockPanel = groupBox_GetParam.Content as DockPanel;
                    if (dockPanel == null)
                    {
                        return;
                    }

                    StackPanel stackPanel = dockPanel.Children[1] as StackPanel;
                    if (stackPanel == null)
                    {
                        return;
                    }

                    foreach (var item in stackPanel.Children)
                    {
                        DockPanel dockPanel_1 = item as DockPanel;
                        if (dockPanel_1 == null)
                        {
                            continue;
                        }


                        TextBox textBox0 = dockPanel_1.Children[0] as TextBox;
                        if (textBox0 == null)
                        {
                            continue;
                        }

                        TextBox textBox3 = dockPanel_1.Children[3] as TextBox;
                        if (textBox3 == null)
                        {
                            continue;
                        }

                        httpClient.AddParam(textBox0.Text, textBox3.Text);
                    }
                }
            }
            httpClient.Start();
        }