处理返回码的部分还需要优化,有现成解决方案的朋友请告诉我,感谢!
1 package com.homework; 2 3 import java.io.IOException; 4 import org.apache.http.HttpResponse; 5 import org.apache.http.client.fluent.Request; 6 7 public class HttpTest { 8 9 public static void main(String[] args) {10 sendURLRequest("http://www.testroad.org/");11 }12 13 static void sendURLRequest(String uri){14 int statusCode = 0;15 HttpResponse response;16 try {17 response=Request.Get(uri).execute().returnResponse();18 statusCode=response.getStatusLine().getStatusCode();19 } catch (IOException e) {20 e.printStackTrace();21 }22 // System.out.println(status);23 if ((200<=statusCode)&&(statusCode<299)) {24 System.out.println(statusCode+": 连接成功");25 }else if ((300<=statusCode)&&(statusCode<399)) {26 System.out.println(statusCode+": 连接成功");27 }else if ((400<=statusCode)&&(statusCode<499)) {28 System.out.println(statusCode+": 连接失败");29 }else if ((500<=statusCode)&&(statusCode<599)) {30 System.out.println(statusCode+": 连接失败");31 }else{32 System.out.println("状态码未知: "+statusCode);33 }34 35 }36 37 }