1:用jdk连接
String action = "xxxxxxxxxxx";
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setConnectTimeout(0);
http.setInstanceFollowRedirects(true);
http.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
http.setDefaultUseCaches(false);
http.setDoOutput(true); String queryString = "";
PrintWriter out = new PrintWriter(http.getOutputStream());
out.print(queryString);//传入参数
out.close();
http.connect();//连接
InputStream in = httpURLConnection.getInputStream();
2:apache组件
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Map;
  5. import org.apache.commons.httpclient.HttpClient;
  6. import org.apache.commons.httpclient.HttpMethod;
  7. import org.apache.commons.httpclient.HttpStatus;
  8. import org.apache.commons.httpclient.URIException;
  9. import org.apache.commons.httpclient.methods.GetMethod;
  10. import org.apache.commons.httpclient.methods.PostMethod;
  11. import org.apache.commons.httpclient.params.HttpMethodParams;
  12. import org.apache.commons.httpclient.util.URIUtil;
  13. /**
  14. *
  15. *
  16. * <p>Title:HttpTookitEnhance</p>
  17. * <p>Description: httpclient模拟http请求,解决返回内容乱码问题</p>
  18. * <p>Copyright: Copyright (c) 2010</p>
  19. * <p>Company: </p>
  20. * @author libin
  21. * @version 1.0.0
  22. */
  23. public class HttpTookitEnhance
  24. {
  25. /**
  26. * 执行一个HTTP GET请求,返回请求响应的HTML
  27. *
  28. * @param url                 请求的URL地址
  29. * @param queryString 请求的查询参数,可以为null
  30. * @param charset         字符集
  31. * @param pretty            是否美化
  32. * @return 返回请求响应的HTML
  33. */
  34. public static String doGet ( String url, String queryString, String charset, boolean pretty )
  35. {
  36. StringBuffer response = new StringBuffer();
  37. HttpClient client = new HttpClient();
  38. GetMethodmethod = new GetMethod(url);
  39. try
  40. {
  41. if ( queryString != null && !queryString.equals("") )
  42. //对get请求参数做了http请求默认编码,好像没有任何问题,汉字编码后,就成为%式样的字符串
  43. method.setQueryString(URIUtil.encodeQuery(queryString));
  44. client.executeMethod(method);
  45. if ( method.getStatusCode() == HttpStatus.SC_OK )
  46. {
  47. BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
  48. String line;
  49. while ( ( line = reader.readLine() ) != null )
  50. {
  51. if ( pretty )
  52. response.append(line).append(System.getProperty("line.separator"));
  53. else
  54. response.append(line);
  55. }
  56. reader.close();
  57. }
  58. }
  59. catch ( URIException e )
  60. {
  61. }
  62. catch ( IOException e )
  63. {
  64. }
  65. finally
  66. {
  67. method.releaseConnection();
  68. }
  69. return response.toString();
  70. }
  71. /**
  72. * 执行一个HTTP POST请求,返回请求响应的HTML
  73. *
  74. * @param url         请求的URL地址
  75. * @param params    请求的查询参数,可以为null
  76. * @param charset 字符集
  77. * @param pretty    是否美化
  78. * @return 返回请求响应的HTML
  79. */
  80. public static String doPost ( String url, Map<String, String> params, String charset, boolean pretty )
  81. {
  82. StringBuffer response = new StringBuffer();
  83. HttpClient client = new HttpClient();
  84. PostMethodmethod = new PostMethod(url);
  85. //设置Http Post数据
  86. if ( params != null )
  87. {
  88. HttpMethodParams p = new HttpMethodParams();
  89. for ( Map.Entry<String, String> entry : params.entrySet() )
  90. {
  91. p.setParameter(entry.getKey(), entry.getValue());
  92. }
  93. method.setParams(p);
  94. }
  95. try
  96. {
  97. client.executeMethod(method);
  98. if ( method.getStatusCode() == HttpStatus.SC_OK )
  99. {
  100. BufferedReader reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), charset));
  101. String line;
  102. while ( ( line = reader.readLine() ) != null )
  103. {
  104. if ( pretty )
  105. response.append(line).append(System.getProperty("line.separator"));
  106. else
  107. response.append(line);
  108. }
  109. reader.close();
  110. }
  111. }
  112. catch ( IOException e )
  113. {
  114. }
  115. finally
  116. {
  117. method.releaseConnection();
  118. }
  119. return response.toString();
  120. }
  121. public static void main ( String [] args )
  122. {
  123. String y = doGet("http://video.sina.com.cn/life/tips.html", null, "GBK", true);
  124. System.out.println(y);
  125. }
  126. }

java模拟get/post提交的更多相关文章

  1. java模拟post方式提交表单实现图片上传【转】

     转自:http://blog.csdn.net/5iasp/article/details/8669644 模拟表单html如下:   <form action="up_result ...

  2. JAVA模拟表单提交

    这是我网上搜的,自己使用也蛮方便,所以上传供大家分享. package wzh.Http;   import java.io.BufferedReader; import java.io.IOExce ...

  3. 真理胜于一切 JAVA模拟表单提交

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  4. Java模拟HttpClient进行Get和Post提交

    使用Java模拟客户端进行提交,需要用到apache http client jar,这里用的是4.4版本 GET: public void GetURL(){  String strResp=&qu ...

  5. java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例

    java模拟表单上传文件,java通过模拟post方式提交表单实现图片上传功能实例HttpClient 测试类,提供get post方法实例 package com.zdz.httpclient; i ...

  6. Java模拟登陆02【转载】

    在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢?     方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时 ...

  7. java模拟form上传数据

    Java模拟form表单上传 查看form表单提交的http请求为 import java.io.*; import java.net.*; public class FileUpload { /** ...

  8. HTTP通信模拟表单提交数据

    前面记录过一篇关于http通信,发送数据的文章:http://www.cnblogs.com/hyyq/p/7089040.html,今天要记录的是如何通过http模拟表单提交数据. 一.通过GET请 ...

  9. java模拟Cookies登陆

    在使用java访问URL时,如果该URL需要身份验证,那么就不能够直接访问,因为没有登陆.那么,如何解决这个问题呢? 方法是使用java模拟登陆,登陆后记录下cookie信息,在下次发起请求时时将co ...

随机推荐

  1. Python 异步IO、IO多路复用

    事件驱动模型 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  2. 国内ip信息库的组建

    1.从 APNIC 分析得到国内的段 数据源位置:http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest 2.从QQ纯真库分析得到国 ...

  3. 《JavaScript 闯关记》之原型及原型链

    原型链是一种机制,指的是 JavaScript 每个对象都有一个内置的 __proto__ 属性指向创建它的构造函数的 prototype(原型)属性.原型链的作用是为了实现对象的继承,要理解原型链, ...

  4. HDU 5618 Jam's problem again

    题意: 三维坐标,对于1个点,找出有多少个点,3个坐标都比该点小! Sample Input 1 4 10 4 7 10 6 6 8 2 5 7 3 10   Sample Output 1 1 0 ...

  5. setNeedsDisplay、layoutSubViews

    UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.而setNeedsDisplay会调 用自动调用drawRect方法,这样可以拿到UIGra ...

  6. SQL重复记录查询的几种方法(转)

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people ) 2.删除表中多余的重复记录,重复记录是根据单个字段(people ...

  7. uva 1595 Symmetry“结构体”

    给出平面上N(N<=1000)个点.问是否可以找到一条竖线,使得所有点左右对称,如图所示: 则左边的图形有对称轴,右边没有.   Sample Input  3 5 -2 5 0 0 6 5 4 ...

  8. git add和被ignore的文件

    如果有如下的目录结构: workspace tree | --------------------- |                             | hello.c           ...

  9. Image的Stride

    参看下面链接:http://msdn.microsoft.com/en-us/library/aa473780

  10. JQuery easyui (2)Droppable(放置)组件

    所谓放置,就是将一个物体放入一个物体内,当然对于easyui来说触发各种效果是必不可少的,同时这个组件也不会依赖于其他组件. Droppable的加载方式 1,class  加载   一直不太喜欢cl ...