Fake a form submission with C# WebClient

后端 未结 6 1047
谎友^
谎友^ 2020-11-30 14:40

I need to call a web and retrieve the resulting data from the model in my asp.net mvc application. When accessed on the web, the form looks like this:



        
6条回答
  •  时光说笑
    2020-11-30 15:21

    Something like this:

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.ContentType = "application/x-www-form-urlencoded";
    string data = "&p=" + dataThatNeedsToBeInTextArea;
    byte[] byteArray = Encoding.UTF8.GetBytes (data);
    req.ContentLength = byteArray.Length;
    Stream stream= req.GetRequestStream ();
    stream.Write (byteArray, 0, byteArray.Length);
    stream.Close ();
    StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
    string response = streamIn.ReadToEnd();
    streamIn .Close(); 
    

提交回复
热议问题