HttpClient使用Post和Get提交参数

  1. package httpclient;
  2. import java.io.IOException;
  3. import java.net.URLEncoder;
  4. import org.apache.commons.httpclient.HttpClient;
  5. import org.apache.commons.httpclient.HttpMethod;
  6. import org.apache.commons.httpclient.NameValuePair;
  7. import org.apache.commons.httpclient.methods.GetMethod;
  8. import org.apache.commons.httpclient.methods.PostMethod;
  9. public class HttpClientTest {
  10. public static void main(String[] args) throws Exception{
  11. String url = "/webservices/DomesticAirline.asmx/getDomesticAirlinesTime";
  12. String host = "www.webxml.com.cn";
  13. String param = "startCity="+URLEncoder.encode("杭州", "utf-8")+"&lastCity=&theDate=&userID=";
  14. HttpClient httpClient = new HttpClient();
  15. httpClient.getHostConfiguration().setHost(host, 80, "http");
  16. HttpMethod method = getMethod(url, param);
  17. //HttpMethod method = postMethod(url);
  18. httpClient.executeMethod(method);
  19. String response = method.getResponseBodyAsString();
  20. //String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"));
  21. System.out.println(response);
  22. }
  23. private static HttpMethod getMethod(String url,String param) throws IOException{
  24. GetMethod get = new GetMethod(url+"?"+param);
  25. get.releaseConnection();
  26. return get;
  27. }
  28. private static HttpMethod postMethod(String url) throws IOException{
  29. PostMethod post = new PostMethod(url);
  30. post.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=gbk");
  31. NameValuePair[] param = { new NameValuePair("startCity","杭州"),
  32. new NameValuePair("lastCity","沈阳"),
  33. new NameValuePair("userID",""),
  34. new NameValuePair("theDate","") } ;
  35. post.setRequestBody(param);
  36. post.releaseConnection();
  37. return post;
  38. }
  39. }
时间: 2024-08-24 01:29:01

HttpClient使用Post和Get提交参数的相关文章

【转】HttpClient使用Post和Get提交参数

package httpclient; import java.io.IOException; import java.net.URLEncoder; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.NameValuePair; import org.apache.common

爬虫:如何破解表单提交参数(FormDate)的网站

在编写爬虫程序的时候,一般的url中会携带页码的参数,例如斗鱼的直播页:https://www.douyu.com/directory/all?page=3&isAjax=1,其中page就代表页码,在爬取的时候只需要利用for循环,将url拼凑完整即可. 但是有些网站的url属于不会变化的,即其参数所在的位置并不存在于url当中,例如该网站:http://113.108.219.40/Dop/Open/EnterpriseList.aspx?,当你翻页的时候url并不会改变. 这时打开F12检

Android 通过GET和POST方式提交参数给web应用

如何把数据上传到web应用 应用界面: 视频名称:title 时长:timelength 保存,点击保存按钮提交到web应用中,web应用中开发Manageservlet来接收数据. get方式 服务端: public class ManageServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request,H

输入值/表单提交参数过滤有效防止sql注入的方法

输入值/表单提交参数过滤,防止sql注入或非法攻击的方法: 代码如下: /** * 过滤sql与php文件操作的关键字 * @param string $string * @return string * @author zrp <[email protected]> */ private function filter_keyword( $string ) { $keyword = select|insert|update|delete|\|\/\*|\*|\.\.\/|\.\/|union

Form 表单提交参数

今天因为要额外提交参数数组性的参数给form传到后台而苦恼了半天,结果发现,只需要在form表单对应的字段html空间中定义name = 后台参数名 的属性就ok了. 后台本来是只有模型参数的,但是后来增加了一个. 只要在前台定义 name="Users" 的select . 点击提交的时候,select 的多选值会自动传入后台参数Users[]中. 已经是第二次忘记这个神奇的作用,特此记下.

Spring MVC url提交参数和获取参数

普通URL提交参数 该格式url为:url.do?param1=mahc&param2=8888.00 需要在上文中的HelloController对象添加方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 /**      * Spring MVC URL提交参数      * @param name      * @return      */     @RequestMapping(/param)     public ModelAndView getInfo(@Request

菜鸟调错(九)——POST方法提交参数丢失问题

在解决一个bug的时候,发现post提交上来的参数,有部分丢失了.经过一番查证,有资料说,Tomcat的server.xml中有一个maxPostSize属性,默认是2M.于是将其值显式置为0(不对post提交的大小做限制). <Connector maxPostSize="0" URIEncoding="utf-8" connectionTimeout="20000" port="8080" protocol=&quo

利用Splatting提交参数(Hash,哈希)

$infos = @{} $infos.Path = 'c:\Windows' $infos.Recurse = $true $infos.Filter = '*.log' $infos.ErrorAction = 'SilentlyContinue' $infos.Remove('Recurse') dir @infos dir c:\windows -rexurse -filter ‘*.log’ -erroraction silentlycontinue 利用Splatting提交参数(H

Spark on Yarn:任务提交参数配置

当在YARN上运行Spark作业,每个Spark executor作为一个YARN容器运行.Spark可以使得多个Tasks在同一个容器里面运行. 以下参数配置为例子: spark-submit --master yarn-cluster   #使用集群调度模式(一般使用这个参数) --num-executors  132      # executor 数量 --executor-cores  2        #设置单个executor能并发执行task数,根据job设置,推荐值2-16 (