我们到底能走多远系列(41)
扯淡:
好久没总结点东西了,技术上没什么总结,感觉做事空牢牢的。最近也比较疲惫。
分享些东西,造福全人类~
主题:
1,java模拟发起一个http请求
/** * post请求 * @param strUrl * @param content * @param charset * @return */ public static String sendPost(String strUrl, String content, String charset) { URL httpurl = null; HttpURLConnection httpConn = null; String returnStr = ""; PrintWriter outs = null; try { httpurl = new URL(strUrl); httpConn = (HttpURLConnection) httpurl.openConnection(); httpConn.setRequestMethod( "POST"); // 默认是post // 设置是否向httpUrlConnection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true, 默认情况下是false; httpConn.setDoOutput( true); // 设置是否从httpUrlConnection读入,默认情况下是true; httpConn.setDoInput( true); httpConn.setRequestProperty( "Content-Type", "text/xml"); outs = new PrintWriter(httpConn.getOutputStream()); outs.print(content); outs.flush(); outs.close(); // 字节流 读取全部内容 包括换行符 returnStr = inputStreamToString(httpConn.getInputStream(), charset); } catch (Exception e) { log.error( "执行HTTP Post请求" + strUrl + "时,发生异常!" , e); if(outs != null){ outs.close(); outs = null; } return returnStr; } finally { if (httpConn != null) httpConn.disconnect(); if(outs != null){ outs.close(); outs = null; } } return returnStr; } public static String inputStreamToString(InputStream in,String encoding) throws Exception{ ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] data = new byte[ BUFFER_SIZE]; int count = -1; while((count = in.read(data,0, BUFFER_SIZE)) != -1) outStream.write(data, 0, count); in.close(); data = null; return new String(outStream.toByteArray(),encoding); }
String line, result = ""; BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream(), "utf-8" )); while ((line = in. readLine()) != null) { result += line + "\n"; } in.close();
public static String sendPost(String url, Map<String, String> params, String charset) { StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(url); method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); method.setRequestHeader("Cookie", "special-cookie=value"); method.setRequestHeader("", ""); // 设置Http Post数据 if (params != null) { HttpMethodParams p = new HttpMethodParams(); for (Map.Entry<String, String> entry : params.entrySet()) { p.setParameter(entry.getKey(), entry.getValue()); } method.setParams(p); } try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), charset)); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); } } catch (IOException e) { log.error("执行HTTP Post请求" + url + "时,发生异常!", e); } finally { method.releaseConnection(); } return response.toString(); }
2,模拟登录微信公众平台

代码类似如下:
public final static String REFERER_H = "Referer"; private LoginResult _login(String username, String pwd) { LoginResult loginResult = new LoginResult(); try { PostMethod post = new PostMethod(LOGIN_URL); post.setRequestHeader("Referer", "https://mp.weixin.qq.com/"); post.setRequestHeader(USER_AGENT_H, USER_AGENT); NameValuePair[] params = new NameValuePair[] { new NameValuePair("username", username), new NameValuePair("pwd", DigestUtils.md5Hex(pwd .getBytes())), new NameValuePair("f", "json"), new NameValuePair("imagecode", "") }; post.setQueryString(params); int status = client.executeMethod(post); if (status == HttpStatus.SC_OK) { String ret = post.getResponseBodyAsString(); LoginJson retcode = JSON.parseObject(ret, LoginJson.class); // System.out.println(retcode.getRet()); if ((retcode.getBase_resp().getRet() == 302 || retcode .getBase_resp().getRet() == 0)) { loginResult.setCookie(client.getState().getCookies()); StringBuffer cookie = new StringBuffer(); // 处理cookies for (Cookie c : client.getState().getCookies()) { cookie.append(c.getName()).append("=") .append(c.getValue()).append(";"); cookiemap.put(c.getName(), c.getValue()); } loginResult.setCookiestr(cookie.toString()); loginResult.setToken(getToken(retcode.getRedirect_url())); loginResult.setLogin(true); return loginResult; }else{ loginResult.setLogin(false); return loginResult; } } } catch (Exception e) { String info = "【登录失败】【发生异常:" + e.getMessage() + "】"; System.err.println(info); log.debug(info); log.info(info); return loginResult; } return loginResult; }
全部代码已经共享出来了:github 希望能帮到你~
3,重定向问题
面试中是不是曾经也被问到过:forword和redirect的区别。
网上的回答也有很多了,回过头来再看看这个问题。其实forword,可以说是服务器发起一个请求,访问自身应用下的资源。因为是服务器自己发起的,所以先前由客户端传递过来的参数就可以保持住了,而且是把访问的这个内部资源返回的内容作为客户端最初请求的响应,所以浏览器的url不会变。
那么redirect的原理:其实是服务端在接到客户端请求后,在响应中的header中加上了一个location的东西,这个东西的原理又是:
让我们继续前行
----------------------------------------------------------------------
努力不一定成功,但不努力肯定不会成功。