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. java与.net比较学习系列(6) 数组

    这一篇文章要总结的内容是数组,数组可以简单地看成是同种数据类型变量的集合,它在我们的开发中经常用到,我们主要从以下几个方面进行总结: 1,数组的创建和初始化 2,数组的访问和遍历 3,数组的总结 数组 ...

  2. 动态SQL的执行,注:exec sp_executesql 其实可以实现参数查询和输出参数的

    本文转自:http://www.cnblogs.com/hnsdwhl/archive/2011/07/23/2114730.html 当需要根据外部输入的参数来决定要执行的SQL语句时,常常需要动态 ...

  3. CBitmap,HBitmap,Bitmap区别及联系

    加载一位图,可以使用LoadImage: HANDLE LoadImage(HINSTANCE hinst,LPCTSTR lpszName,UINT uType,int cxDesired,int ...

  4. Linux内核:sk_buff解析

    sk_buff 目录 1 sk_buff介绍 2 sk_buff组成 3 struct sk_buff 结构体 4 sk_buff成员变量 4.1 Layout布局 4.2 General通用 4.3 ...

  5. Nohttp网络请求数据,Post以及Get的简单实用以及设置缓存文字的的请求

    开局声明:这是基于nohttp1.0.4-include-source.jar版本写的教程 由于nohttp功能强悍,因此需要多种权限,仅仅一个联网的权限是不够的,如果只给了Internet的权限,去 ...

  6. PHP学习笔记三十二【Exception】

    <?php // $fp=fopen("a.txt","r"); // echo "ok"; if(!file_exists(&quo ...

  7. ORACLE/MYSQL/DB2等不同数据库取前几条记录

    选取数据库中记录的操作是最基础最频繁的,但往往实际应用中不会这么简单,会在选取记录的时候加上一些条件,比如取前几条记录,下面就总结了如何在ORACLE/MYSQL/DB2等一些热门数据库中执行取前几条 ...

  8. codeforces 339C Xenia and Bit Operations(线段树水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Xenia and Bit Operations Xenia the beginn ...

  9. C++程序设计实践指导1.1删除序列中相同的数改写要求实现

    改写要求1:改写为以指针为数据结构 #include <iostream> #include <cstdlib> using namespace std; class ARP ...

  10. (原+译)win7远程连接ubuntu16.04

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5711214.html 原始网址: http://ubuntuhandbook.org/index.ph ...