java模拟get/post提交

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. }
上一篇:Linux Vi 删除全部内容,删除某行到结尾,删除某段内容 的方法


下一篇:HDU3487 Play With Chains(Splay)