如何模拟POST请求?

use*_*380 27 http-post

我正在Windows上测试,尝试模拟POST请求(使用不同的表单变量)进行负载测试.我已经尝试了各种负载测试软件,但未能使其正常工作.

对于GET请求,我知道我可以将参数放在url后面

http://www.example.com?id=yyy&t=zzz

但是我该如何模拟POST请求呢?

我有一个Chrome REST客户端,但我不知道要在头文件和数据中添加什么.

这是我到目前为止所尝试的:

class Program
    {
        static void Main(string[] args)
        {

            string viewstateid = "/wEPDwUKLTY3NjEyMzE4NWRkK4DxZpjTmZg/RGCS2s13vkEWmwWiEE6v+XrYoWVuxeg=";
            string eventid ="/wEdAAoSjOGPZYAAeKGjkZOhQ+aKHfOfr91+YI2XVhP1c/pGR96FYSfo5JULYVvfQ61/Uw4pNGL67qcLo0vAZTfi8zd7jfuWZzOhk6V/gFA/hhJU2fx7PQKw+iST15SoB1LqJ4UpaL7786dp6laCBt9ubQNrfzeO+rrTK8MaO2KNxeFaDhrQ0hxxv9lBZnM1SHtoODXsNUYlOeO/kawcn9fX0BpWN7Brh7U3BIQTZwMNkOzIy+rv+Sj8XkEEA9HaBwlaEjg=";

            string username = "user1";
            string password = "ttee";

            string loginbutton = "Log In";

            string URLAuth = "http://localhost/login.aspx";
            string postString = string.Format("VIEWSTATE={0}&EVENTVALIDATION={1}&LoginUser_UserName={2}&LoginUser_Password={3}&LoginUser_LoginButton={4}",viewstateid,eventid, username, password,realm,otp,loginbutton);

            const string contentType = "application/x-www-form-urlencoded";
            System.Net.ServicePointManager.Expect100Continue = false;

            CookieContainer cookies = new CookieContainer();
            HttpWebRequest webRequest = WebRequest.Create(URLAuth) as HttpWebRequest;
            webRequest.Method = "POST";
            webRequest.ContentType = contentType;
            webRequest.CookieContainer = cookies;
            webRequest.ContentLength = postString.Length;
            webRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1";
            webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

           webRequest.Referer = "http://localhost/login.aspx";

            StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
            requestWriter.Write(postString);
            requestWriter.Close();

            StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
            string responseData = responseReader.ReadToEnd();
            Console.WriteLine(responseData);
            responseReader.Close();
            webRequest.GetResponse().Close();

        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 18

如果您提供了更多信息会很有帮助 - 例如您使用的操作系统,您想要完成的操作等等.但是,一般来说,cURL是一个非常强大的命令行工具,我经常使用(在linux中)来模仿HTML请求:

例如:

curl --data "post1=value1&post2=value2&etc=valetc" http://host/resource
Run Code Online (Sandbox Code Playgroud)

或者,对于RESTful API:

curl -X POST -d @file http://host/resource
Run Code Online (Sandbox Code Playgroud)

您可以在这里查看更多信息 - > http://curl.haxx.se/


编辑:

好.所以基本上你想要对你的REST服务器进行压力测试?然后cURL真的没用,除非你想编写自己的负载测试程序,即使那时套接字也是要走的路.我建议你看看加特林.Gatling文档解释了如何设置工具,从那里你可以运行各种GET,POST,PUT和DELETE请求.

不幸的是,如果没有编写自己的程序 - 即产生一大堆线程并用不同类型的请求淹没REST服务器 - 你真的必须依赖压力/负载测试工具包.仅使用REST客户端发送请求不会给您的服务器带来太大压力.


更多编辑

因此,为了模拟套接字上的post请求,您基本上必须与服务器构建初始套接字连接.我不是C#家伙,所以我不能告诉你到底该怎么做; 我确信网上有1001个C#套接字教程.对于大多数RESTful API,您通常需要提供一个URI来告诉服务器该做什么.例如,假设您的API管理一个库,并且您正在使用POST请求来告知服务器更新有关ID为"34"的图书的信息.您的URI可能是

http://localhost/library/book/34
Run Code Online (Sandbox Code Playgroud)

因此,您应该在端口80(或8080,或您的服务器所在的任何端口)上打开与localhost的连接,并传递HTML请求标头.使用上面的库示例,您的请求标头可能如下所示:

POST library/book/34 HTTP/1.0\r\n
X-Requested-With: XMLHttpRequest\r\n
Content-Type: text/html\r\n
Referer: localhost\r\n
Content-length: 36\r\n\r\n
title=Learning+REST&author=Some+Name
Run Code Online (Sandbox Code Playgroud)

从这里,服务器应该回击一个响应头,然后是编程的API,告诉客户端 - 通常说POST成功或失败.要对API进行压力测试,您应该通过创建线程化过程来反复执行此操作.

此外,如果您要发布JSON数据,则必须相应地更改标题和内容.坦率地说,如果你想快速干净,我建议使用python(或perl),它有几个库来创建POST,PUT,GET和DELETE请求,以及POSTing和PUTing JSON数据.否则,您最终可能会进行比压力测试更多的编程.希望这可以帮助!


Lun*_*una 9

Postman是测试API的最佳应用程序!

您可以导入或导出您的路线,让他记住您的所有身体要求!:)


https://chrome.google.com/webstore/detail/postman-rest-client-packa/fhbjgbiflinjbdggehcddcbncdddomop


ken*_*orb 6

简单的方法是curl从命令行使用,例如:

DATA="foo=bar&baz=qux"
curl --data "$DATA" --request POST --header "Content-Type:application/x-www-form-urlencoded" http://example.com/api/callback | python -m json.tool
Run Code Online (Sandbox Code Playgroud)

或者这里是如何使用Bash shell发送原始POST请求的示例(JSON请求):

exec 3<> /dev/tcp/example.com/80

DATA='{"email": "foo@example.com"}'
LEN=$(printf "$DATA" | wc -c)

cat >&3 << EOF
POST /api/retrieveInfo HTTP/1.1
Host: example.com
User-Agent: Bash
Accept: */*
Content-Type:application/json
Content-Length: $LEN
Connection: close

$DATA
EOF

# Read response.
while read line <&3; do
   echo $line
done
Run Code Online (Sandbox Code Playgroud)