C#HttpClient不发送POST变量

时间:2015-03-31 18:18:54

标签: c# post dotnet-httpclient

我正在尝试制作一个应用程序,它会向https://owlexpress.kennesaw.edu/prodban/bwckschd.p_get_crse_unsec发送一个POST请求,并提供一些信息并返回一个班级列表。

你可以到这里进行搜索“我正在使用2015秋季,MATH,课程1190”。 https://owlexpress.kennesaw.edu/prodban/bwckschd.p_disp_dyn_sched

当我运行下面的代码时,它输出返回到进入webbrowser组件的字符串的内容。它显示:

Class Schedule Search    
Fall Semester 2015
Mar 31, 2015
Stop You must select at least ONE subject . 

我使用Chrome调试来查找POST值并将其设置为我正常使用网站时的值。我甚至在需要的时候加了一些饼干。

编辑:

好的,新问题。我使用了一个有效的浏览器:“term_in = 201508& sel_subj = dummy& sel_day = dummy& sel_schd = dummy& sel_insm = dummy& sel_c amp = dummy& sel_levl = dummy& sel_sess = dummy& sel_instr = dummy& sel_ptrm =虚设&安培; sel_attr = d ummy&安培; sel_subj = MATH&安培; sel_crse = 1190&安培; sel_title =安培; sel_insm =%25安培; sel_from_cred =安培; sel_to_cr ED =&安培; sel_camp =%25安培; sel_levl =%25安培; sel_ptrm =%25安培; sel_instr =%25& begin_hh = 0& begin_mi = 0& be gin_ap = a& end_hh = 0& end_mi = 0& end_ap = a“

我不能发送它,因为它使用了一些名称两次,比如sel_subj(这些是我在字典中的键值)

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Http;
using System.Net;

namespace ClassChecker
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    methods methods1 = new methods();

    public MainWindow()
    {
        InitializeComponent();

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {            
        string temp = methods1.getData2();
        Console.ReadLine();
        webBrowser.NavigateToString(temp);            
    }
}


public class methods
{

    public string getData2()
    {
        var cookieContainer = new CookieContainer();
        using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
        using (var client = new HttpClient(handler))
        {
            client.BaseAddress = new Uri("https://owlexpress.kennesaw.edu");

             var values = new Dictionary<string, string>
            {
               { "sel_subj", "MATH" },
               { "term_in", "201508" },
               { "sel_day", "dummy"},
               { "sel_schd", "dummy"},
               { "sel_insm", "%"},
               { "sel_camp", "%"},
               { "sel_levl", "%"},
               { "sel_sess", "dummy"},
               { "sel_instr", "%"},
               { "sel_ptrm", "%"},
               { "sel_attr", "dummy"},                   
               { "sel_crse", "1190" },
               { "sel_title", "" },
               { "sel_from_cred", "" },
               { "sel_to_cred", "" },
               { "begin_hh", "0" },
               { "begin_mi", "0" },
               { "begin_ap", "a" },
               { "end_hh", "0" },
               { "end_mi", "0" },
               { "end_ap", "a" }

            };
            var content = new FormUrlEncodedContent(values);

            cookieContainer.Add(client.BaseAddress, new Cookie("SESSID", "MFlIU0VSMTgxNjYx"));
            cookieContainer.Add(client.BaseAddress, new Cookie("BIGipServerowlexpress-all", "2239289986.0.0000"));

            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/html"));
            var result = client.PostAsync("/prodban/bwckschd.p_get_crse_unsec", content).Result;
            string resultContent = result.Content.ReadAsStringAsync().Result;                
            MessageBox.Show(result.Headers.ToString());
            return resultContent;
        }

    }

}

}

1 个答案:

答案 0 :(得分:2)

根据我们在评论中的讨论,您要发布的服务需要一组带有“虚拟”值的参数和另一组具有实际值的相同参数。

如果对进入FormUrlEncodedContent构造函数的参数使用NameValueCollection,则可以使用重复键。它应该工作