如何以编程方式模拟HTTP POST在ASP.NET? [英] How to simulate HTTP POST programatically in ASP.NET?

查看:111
本文介绍了如何以编程方式模拟HTTP POST在ASP.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式模拟HTTP POST,即我需要生成一些POST变量请求,然后将其发送到网页。

I need to simulate an HTTP POST programatically, i.e., I need to generate a Request with some POST variables and then send it to a page.

要澄清一下,我需要模拟一个普通POST的行为,而不是做整个事情编程。所以基本上我需要填写同样的方式,将一个表单POST发生的一切充满一个请求,然后发送给浏览器的一个期望POST的页面。

To clarify, I need to simulate the behaviour of a regular POST, not do the whole thing programatically. So basically I need to fill in a Request in the same way it would be filled if a form POST was happening, and then send the browser to the page that expects the POST.

推荐答案

下面是做到这一点的方法之一。

Here is one way to do it.

您发送该方法的URL,并在NameValueCollection中的形式名称/值参数。该方法使一个HTTP POST在端点上并返回响应作为一个字符串。

You send this method the url and the name/value parameters in the form of a NameValueCollection. The method makes a Http Post on the endpoint and returns the response as a string.

当然取决于什么/你为什么这样做多少次这样的方法将被调用,也许还有其他选择。但是,直到您提供您的具体需要了解更多信息,这种方法是不够好。

Of course depending on what/why you're doing this and how many times this method will be called, there maybe other alternatives. But until you provide more information on your specific needs, this method is good enough.

下面的方法使用任务(.NET 4.0)和异步方法,所以它会更快然后像下一个code房源的同步方法,如果你正在做的,例如一个循环多次调用。

The method below uses Tasks (.NET 4.0) and the async methods so it will be faster then a synchronous method like the next code listing if you're making multiple calls in a loop for example.

static string GetWebResponse(string url, NameValueCollection parameters)
{
  var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
  httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  httpWebRequest.Method = "POST";

  var sb = new StringBuilder();
  foreach (var key in parameters.AllKeys)
    sb.Append(key + "=" + parameters[key] + "&");
  sb.Length = sb.Length - 1;

  byte[] requestBytes = Encoding.UTF8.GetBytes(sb.ToString());
  httpWebRequest.ContentLength = requestBytes.Length;

  using (var requestStream = httpWebRequest.GetRequestStream())
  {
    requestStream.Write(requestBytes, 0, requestBytes.Length);
    requestStream.Close();
  }

  Task<WebResponse> responseTask = Task.Factory.FromAsync<WebResponse>(httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, null);
  using (var responseStream = responseTask.Result.GetResponseStream())
  {
    var reader = new StreamReader(responseStream);
    return reader.ReadToEnd();
  }
}

您也可以使用Web客户端(它的简单一点)。这种方法需要在表单中的POST参数作为一个字符串

you could also use WebClient (it's bit simpler). This method expects the post parameters as a string in the form

name1=value1&name2=value2&name3=value3

等。所以,如果你用这个方法一定要在你的参数,例如通过或修改的实施是像上面的code。

etc. So if you use this method be sure to pass in your parameters as such or modify the implementation to be like the code above.

static string HttpPost(string url, string Parameters) 
{
   var req = System.Net.WebRequest.Create(url);       

   req.ContentType = "application/x-www-form-urlencoded";
   req.Method = "POST";
   byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
   req.ContentLength = bytes.Length;
   System.IO.Stream os = req.GetRequestStream ();
   os.Write (bytes, 0, bytes.Length);
   os.Close ();
   System.Net.WebResponse resp = req.GetResponse();
   if (resp== null)
     return null;
   var sr = new StreamReader(resp.GetResponseStream());
   return sr.ReadToEnd().Trim();
}

这篇关于如何以编程方式模拟HTTP POST在ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆