本文介绍了C#HttpClient不发送POST变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个将POST请求发送到 https://owlexpress的应用.kennesaw.edu/prodban/bwckschd.p_get_crse_unsec 并提供一些信息,然后返回课程列表.

I'm trying to make an app that will send a POST request to https://owlexpress.kennesaw.edu/prodban/bwckschd.p_get_crse_unsec with some info and return a class list.

您可以在这里进行搜索我正在使用2015年秋季的MATH,课程1190". https://owlexpress.kennesaw.edu/prodban/bwckschd.p_disp_dyn_sched

You can go here to go through the search "I'm using Fall 2015, MATH, Course 1190".https://owlexpress.kennesaw.edu/prodban/bwckschd.p_disp_dyn_sched

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

When I run the code below, it outputs what it returns to a string which goes into a webbrowser component. It shows:

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

我使用Chrome调试来查找POST值,并将其设置为正常使用网站时的值.我什至包括了一些cookie,以防万一需要它们.

I used Chrome Debugging to find the POST values and set them to what they are when I use the site normally. I even included some cookies in case it needed those.

好,新一期.我使用了一个可以正常工作的浏览器,并获得了以下信息:"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_insstr = pt = dummy& 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 ; sel_instr =%25& begin_hh = 0& begin_mi = 0& be‌́gin_ap = a& end_hh = 0& end_mi = 0& end_ap = a"

Ok, new issue. I used a browser that works and got this: "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=dummy&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(这些是我在字典中的键值)

I can't send that though because is uses some names twice, like sel_subj (and those are my key values in the dictionary)

代码:

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

    }

}

}

推荐答案

根据我们在注释中的讨论,您要发布到的服务需要一组带有虚拟"值的参数以及另一组具有真实值的相同参数.

Per our discussion in comments, the service you are posting to requires a set of parameters with 'dummy' values AND another set of the same params with real values.

如果使用NameValueCollection作为进入FormUrlEncodedContent构造函数的参数,则可以使用重复键.然后应该可以工作

If you use NameValueCollection for the argument going into the FormUrlEncodedContent constructor, you will be able to use duplicate keys. It should then work

这篇关于C#HttpClient不发送POST变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 23:18