• Java的HttpClient的实现


    HttpClient的概念就是模仿浏览器请求服务端内容,也可以做App和Server之间的链接。

    这个是关于Java的HttpClient的简单实例,其实java本身也可以通过自己的net包去做,但是在应用上还是HttpClient方便很多

    地址是:http://hc.apache.org/downloads.cgi 
    下面是java的实例(这里使用了json的jar包,可以前往http://mvnrepository.com/artifact/org.json/json去寻找下载):

    注意:jdk1.8

    import java.io.IOException;
    
    import org.apache.http.Header;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.json.JSONTokener;
    
    public class WebSiteRequest {
        public static void main(String args[]) {
            String reqUri = "https://xxx.com";
            String parm = "data=0_0_1"
                           
            CloseableHttpClient httpclient = HttpClients.createDefault();
            try {
                HttpResponse response = null;
                HttpPost httpPost = new HttpPost(reqUri + "?" + parm);
                Header ContentTypeHeader = new BasicHeader("Content-Type", "application/x-www-form-urlencoded");
                httpPost.addHeader(ContentTypeHeader);
                try {
                    response = httpclient.execute(httpPost);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                int statusCode = response.getStatusLine().getStatusCode();
                String getResult = null;
                if (statusCode != HttpStatus.SC_OK) {
                    System.out.println("error status code: " + statusCode);
                } else {
                    try {
                        getResult = EntityUtils.toString(response.getEntity());
                    } catch (IOException ioException) {
                        ioException.printStackTrace();
                    }
                }
                if (getResult != null) {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
                        System.out.println("retcode: " + jsonObject.getString("retcode"));
                    } catch (JSONException jsonException) {
                        jsonException.printStackTrace();
                    }
                }
            } finally {
                try {
                    httpclient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    Android中的“再按一次返回键退出程序”代码实现
    Android UI编程之自定义控件初步——ImageButton
    21岁,我想当“大帅”
    茑萝改变了我
    茑萝,梦想的加油站
    放弃了我的国企工作
    性能调优之访问日志IO性能优化
    性能调优之提高 ASP.NET Web 应用性能的 24 种方法和技巧
    性能调优之剖析OutOfMemoryError
    老李分享:单元测试检查清单:让测试有效,避免致命错误
  • 原文地址:https://www.cnblogs.com/zzss/p/5778676.html
Copyright © 2020-2023  润新知