如何以编程方式发送带参数的HTTP请求? [英] How to programmatically send a HTTP request with parameters?

查看:153
本文介绍了如何以编程方式发送带参数的HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用浏览器向服务器发送信息(例如使用登录,密码页面),我只需填写用户文本框和密码文本框,然后单击登录按钮。

If I use a browser to send information to the server (for example using a log-in, password page), I just fill the user text-box and the password text-box and clicking on the log-in button.

我想发送此信息,但无需使用浏览器。我想'填充'文本框,但无需在浏览器中手动完成。可能正在使用Servlet。

I would like to send this information but without having to use the browser. I would like to 'fill' the text-boxes but without having to do it manually in the browser. May be using a Servlet.

我的问题是:如何在文本框中发送信息,例如,从Servlet发送到网站?

My question is: How to send information in text-boxes, for example, to a website, doing it from a Servlet?

推荐答案

为什么不只是打电话给 URL 使用类似 http://your.domain.name/your/servlet/path?userFieldName=THE_VALUE_YOU_WANT_TO_PASS&passwdFieldName=PASSWORD

why not just make a call to the URL from Java using a URL like http://your.domain.name/your/servlet/path?userFieldName=THE_VALUE_YOU_WANT_TO_PASS&passwdFieldName=PASSWORD

servlet会感觉价值来自那些盒子。

The servlet will feel like the values are coming from those boxes.

或者你可能想潜入 Apache HTTP Client 模仿客户发送的请求。

Or you may want to dive into Apache HTTP Client to mimick a request sent from an client.

呃......你在做功能测试吗?为什么不查看 JMeter

uh..oh.. are you doing functional testing? Why not look into JMeter?

根据评论进行更新

您需要知道实际表单提交的内容是什么?它基本上形成了一个由键值(KV)对组成的查询字符串。

You need to know what actually form submission does? It basically forms a query string composed of Key-Values (KV) pair.

所以,如果你有一个名为 tfield 的文本字段,其中用户键入了一些文字,并且有一个名为 ddfield 的下拉列表,其中用户已选择 optionX optionX-Val 。并且此表单将提交到URL, http://my.domain.name/my/servlet - 浏览器将发送一个看起来像

So, if you have a a text field named tfield where user has typed some text, and there is a drop down named, ddfield where user has selected optionX which has value optionX-Val. And this form gets submitted to a URL, http://my.domain.name/my/servlet -- the browser will send a request which will look like

http://my.domain.name/my/servlet?tfield=some%20text&ddfield=optionX-Val

如果要模仿表单提交,则必须手动创建一个包含所有字段的请求字符串的URL它们的值为FIELD_NAME = FIELDVALUE有序对由&符号分隔(&)

If you want to mimic form submission, you will have to manually create a URL that has a request string containing all the fields and their values as FIELD_NAME=FIELDVALUE ordered pair separated by ampersand (&)

啊,好主意。如果您使用Firebug(Firefox扩展),请在Firebug中打开NET面板,手动提交您想要模仿的表单。查看提交表单时发布的请求。它将具有您所追求的确切URL格式。复制此URL,替换值并根据需要进行虚假提交。

ah, great idea. If you use Firebug (a Firefox extension), open the NET panel in Firebug, make a manual submission of the form that you wanted to mimic. See what request is posted when you submitted the form. It will have exact URL format that you are after. Copy this URL, replace the values and make fake submissions as much as you want.

希望这会有所帮助。

这篇关于如何以编程方式发送带参数的HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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