<div class="blogpost-body" id="cnblogs_post_body">
<p><span style="color:#000000;font-size:18px;"> 在java代码中,使用HttpClient爬取https页面时,遇到了这个bug:<span style="color:#ff0000;">javax.net.ssl.SSLException: Received fatal alert: protocol_version</span></span><br><br></p>
<p> <span style="font-size:18px;"> 先奉上初始的代码:</span></p>
<div class="cnblogs_code">
<pre class="blockcode"><span style="color:#008080;"> 1</span> <span style="color:#008000;">/**</span>
<span style="color:#008080;"> 2</span> <span style="color:#008000;"> *
</span><span style="color:#008080;"> 3</span> <span style="color:#008000;">*/</span>
<span style="color:#008080;"> 4</span> <span style="color:#0000ff;">package</span><span style="color:#000000;"> com.tcl.mibc.weathercrawler;
</span><span style="color:#008080;"> 5</span>
<span style="color:#008080;"> 6</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.HttpEntity;
</span><span style="color:#008080;"> 7</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.HttpException;
</span><span style="color:#008080;"> 8</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.HttpResponse;
</span><span style="color:#008080;"> 9</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.client.HttpClient;
</span><span style="color:#008080;">10</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.client.methods.HttpGet;
</span><span style="color:#008080;">11</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.client.methods.HttpRequestBase;
</span><span style="color:#008080;">12</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.impl.client.HttpClients;
</span><span style="color:#008080;">13</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.protocol.BasicHttpContext;
</span><span style="color:#008080;">14</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.protocol.HttpContext;
</span><span style="color:#008080;">15</span> <span style="color:#0000ff;">import</span><span style="color:#000000;"> org.apache.http.util.EntityUtils;
</span><span style="color:#008080;">16</span>
<span style="color:#008080;">17</span> <span style="color:#008000;">/**</span>
<span style="color:#008080;">18</span> <span style="color:#008000;"> * </span><span style="color:#808080;">@author</span><span style="color:#008000;"> yanzhou
</span><span style="color:#008080;">19</span> <span style="color:#008000;"> *
</span><span style="color:#008080;">20</span> <span style="color:#008000;">*/</span>
<span style="color:#008080;">21</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">class</span><span style="color:#000000;"> PageOld {
</span><span style="color:#008080;">22</span>
<span style="color:#008080;">23</span> <span style="color:#008000;">/**</span>
<span style="color:#008080;">24</span> <span style="color:#008000;"> * </span><span style="color:#808080;">@param</span><span style="color:#008000;"> args
</span><span style="color:#008080;">25</span> <span style="color:#008000;">*/</span>
<span style="color:#008080;">26</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> main(String[] args) {
</span><span style="color:#008080;">27</span> System.setProperty("javax.net.debug", "all"<span style="color:#000000;">);
</span><span style="color:#008080;">28</span> String url = "https://www.timeanddate.com/weather/"<span style="color:#000000;">;
</span><span style="color:#008080;">29</span> HttpClient client =<span style="color:#000000;"> HttpClients.createDefault();
</span><span style="color:#008080;">30</span> HttpRequestBase http = <span style="color:#0000ff;">new</span><span style="color:#000000;"> HttpGet(url);
</span><span style="color:#008080;">31</span> HttpContext context = <span style="color:#0000ff;">new</span><span style="color:#000000;"> BasicHttpContext();
</span><span style="color:#008080;">32</span> <span style="color:#0000ff;">try</span><span style="color:#000000;"> {
</span><span style="color:#008080;">33</span> HttpResponse response =<span style="color:#000000;"> client.execute(http, context);
</span><span style="color:#008080;">34</span> <span style="color:#0000ff;">int</span> statusCode =<span style="color:#000000;"> response.getStatusLine().getStatusCode();
</span><span style="color:#008080;">35</span>
<span style="color:#008080;">36</span> <span style="color:#0000ff;">switch</span><span style="color:#000000;"> (statusCode) {
</span><span style="color:#008080;">37</span> <span style="color:#0000ff;">case</span> 200<span style="color:#000000;">:
</span><span style="color:#008080;">38</span> <span style="color:#0000ff;">case</span> 400:<span style="color:#008000;">//</span><span style="color:#008000;"> 业务异常</span>
<span style="color:#008080;">39</span> <span style="color:#0000ff;">break</span><span style="color:#000000;">;
</span><span style="color:#008080;">40</span> <span style="color:#0000ff;">default</span> |
|