HttpClient

C# Httpclient编程 今天研究了一天如何添加httpcient 阅读详情
1:概述  

HttpClient是HttpComponents(简称为hc)项目其中的一部份,访问地址:http://hc.apache.org/ 


HttpClient是一个代码级的Http客户端工具,可以使用它模拟浏览器向Http服务器发送请求。使用HttpClient还需要HttpCore.后者包括Http请求与Http响应的代码封装。 

 

2:HttpGet 

Java代码   收藏代码
  1. public final static void main(String[] args) throws Exception {  
  2.     HttpClient httpclient = new DefaultHttpClient();  
  3.     try {  
  4.         HttpGet httpget = new HttpGet("http://www.apache.org/");  
  5.         System.out.println("executing request " + httpget.getURI());  
  6.         HttpResponse response = httpclient.execute(httpget);  
  7.         HttpEntity entity = response.getEntity();  
  8.   
  9.         System.out.println("----------------------------------------");  
  10.         System.out.println(response.getStatusLine());  
  11.         if (entity != null) {  
  12.             System.out.println("Response content length: " + entity.getContentLength());  
  13.         }  
  14.         System.out.println("----------------------------------------");  
  15.   
  16.         InputStream inSm = entity.getContent();  
  17.         Scanner inScn = new Scanner(inSm);  
  18.         while (inScn.hasNextLine()) {  
  19.             System.out.println(inScn.nextLine());  
  20.         }  
  21.         // Do not feel like reading the response body  
  22.         // Call abort on the request object  
  23.         httpget.abort();  
  24.     } finally {  
  25.         // When HttpClient instance is no longer needed,  
  26.         // shut down the connection manager to ensure  
  27.         // immediate deallocation of all system resources  
  28.         httpclient.getConnectionManager().shutdown();  
  29.     }  
  30. }  




httpcore:EntityUtils.toString(httpEntity)  

读取response响应内容部分,也可以借助于 httpcore-4.1.2.jar 里面的 
Java代码   收藏代码
  1. String content = EntityUtils.toString(httpEntity);  


具体: 
Java代码   收藏代码
  1. /** 
  2.  * User: liuwentao 
  3.  * Time: 12-1-25 下午1:23 
  4.  */  
  5. public class Demo1 {  
  6.   
  7.     /** 
  8.      * 用 get 方法访问 www.apache.org 并返回内容 
  9.      * 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         //创建默认的 HttpClient 实例  
  14.         HttpClient httpClient = new DefaultHttpClient();  
  15.         try {  
  16.             //创建 httpUriRequest 实例  
  17.             HttpGet httpGet = new HttpGet("http://www.apache.org/");  
  18.             System.out.println("uri=" + httpGet.getURI());  
  19.   
  20.             //执行 get 请求  
  21.             HttpResponse httpResponse = httpClient.execute(httpGet);  
  22.   
  23.             //获取响应实体  
  24.             HttpEntity httpEntity = httpResponse.getEntity();  
  25.             //打印响应状态  
  26.             System.out.println(httpResponse.getStatusLine());  
  27.             if (httpEntity != null) {  
  28.                 //响应内容的长度  
  29.                 long length = httpEntity.getContentLength();  
  30.                 //响应内容  
  31.                 String content = EntityUtils.toString(httpEntity);  
  32.   
  33.                 System.out.println("Response content length:" + length);  
  34.                 System.out.println("Response content:" + content);  
  35.             }  
  36.   
  37.             //有些教程里没有下面这行  
  38.             httpGet.abort();  
  39.         } catch (Exception e) {  
  40.             e.printStackTrace();  
  41.         } finally {  
  42.             //关闭连接,释放资源  
  43.             httpClient.getConnectionManager().shutdown();  
  44.         }  
  45.     }  
  46. }  


执行结果: 



httpget.setHeader  

1:分析请求包中这六个头信息 

 

2:代码 
Java代码   收藏代码
  1. /** 
  2.  * @param args 
  3.  * @throws IOException 
  4.  */  
  5. public static void main(String[] args) {  
  6.     HttpClient httpClient = new DefaultHttpClient();  
  7.     try {  
  8.         HttpGet httpget = new HttpGet("http://www.iteye.com");  
  9.   
  10.         httpget.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");  
  11.         httpget.setHeader("Accept-Language", "zh-cn,zh;q=0.5");  
  12.         httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)");  
  13.         httpget.setHeader("Accept-Encoding", "gzip, deflate");  
  14.         httpget.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");  
  15.         httpget.setHeader("Host", "www.iteye.com");  
  16.         httpget.setHeader("Connection", "Keep-Alive");  
  17.   
  18.         HttpResponse response = httpClient.execute(httpget);  
  19.         HttpEntity entity = response.getEntity();  
  20.   
  21.         System.out.println("----------------------------------------");  
  22.         System.out.println(response.getStatusLine());  
  23.         if (entity != null) {  
  24.             System.out.println("Response content length: " + entity.getContentLength());  
  25.         }  
  26.         System.out.println("----------------------------------------");  
  27.   
  28.         InputStream inSm = entity.getContent();  
  29.         Scanner inScn = new Scanner(inSm);  
  30.         while (inScn.hasNextLine()) {  
  31.             System.out.println(inScn.nextLine());  
  32.         }  
  33.         // Do not feel like reading the response body  
  34.         // Call abort on the request object  
  35.         httpget.abort();  
  36.     } catch (Exception e) {  
  37.         e.printStackTrace();  
  38.     } finally {  
  39.         // When HttpClient instance is no longer needed,  
  40.         // shut down the connection manager to ensure  
  41.         // immediate deallocation of all system resources  
  42.         httpClient.getConnectionManager().shutdown();  
  43.     }  
  44. }  

3:效果 



以上乱码是由于 
Java代码   收藏代码
  1. httpget.setHeader("Accept-Encoding", "gzip, deflate");    

注释掉这行,具体: 
Java代码   收藏代码
  1. /** 
  2.  * User: liuwentao 
  3.  * Time: 12-1-25 下午1:23 
  4.  */  
  5. public class Demo3 {  
  6.   
  7.     /** 
  8.      * 用 get 方法访问 www.baidu.com 并返回内容 
  9.      * 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         //创建默认的 HttpClient 实例  
  14.         HttpClient httpClient = new DefaultHttpClient();  
  15.   
  16.         try {  
  17.             //创建 httpUriRequest 实例  
  18.             HttpGet httpGet = new HttpGet("http://www.iteye.com");  
  19.   
  20.             httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");  
  21.             httpGet.setHeader("Accept-Language", "zh-cn,zh;q=0.5");  
  22.             httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1)");  
  23. //            httpGet.setHeader("Accept-Encoding", "gzip, deflate");  
  24.             httpGet.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7");  
  25.             httpGet.setHeader("Host", "www.iteye.com");  
  26.             httpGet.setHeader("Connection", "Keep-Alive");  
  27.             System.out.println("uri=" + httpGet.getURI());  
  28.   
  29.             //执行 get 请求  
  30.             HttpResponse httpResponse = httpClient.execute(httpGet);  
  31.   
  32.             //获取响应实体  
  33.             HttpEntity httpEntity = httpResponse.getEntity();  
  34.             //打印响应状态  
  35.             System.out.println(httpResponse.getStatusLine());  
  36.             if (httpEntity != null) {  
  37.                 //响应内容的长度  
  38.                 long length = httpEntity.getContentLength();  
  39.                 //响应内容  
  40.                 String content = EntityUtils.toString(httpEntity);  
  41.   
  42.                 System.out.println("Response content length:" + length);  
  43.                 System.out.println("Response content:" + content);  
  44.             }  
  45.   
  46.             //有些教程里没有下面这行  
  47.             httpGet.abort();  
  48.         } catch (Exception e) {  
  49.             e.printStackTrace();  
  50.         } finally {  
  51.             //关闭连接,释放资源  
  52.             httpClient.getConnectionManager().shutdown();  
  53.         }  
  54.     }  
  55. }  


效果: 


用post方法访问本地应用根据传递参数不同,返回不同结果  

web.xml 
Java代码   收藏代码
  1. <servlet>  
  2.     <servlet-name>Test1Servlet</servlet-name>  
  3.     <servlet-class>demo.servlet.Test1Servlet</servlet-class>  
  4. </servlet>  
  5. <servlet-mapping>  
  6.     <servlet-name>Test1Servlet</servlet-name>  
  7.     <url-pattern>/test1Servlet</url-pattern>  
  8. </servlet-mapping>  


Test1Servlet.java 
Java代码   收藏代码
  1. /** 
  2.  * User: liuwentao 
  3.  * Time: 11-12-25 下午1:08 
  4.  */  
  5. public class Test1Servlet extends HttpServlet {  
  6.     @Override  
  7.     protected void doPost(HttpServletRequest request, HttpServletResponse response)  
  8.             throws ServletException, IOException {  
  9.         //接收地址栏参数  
  10.         String param1 = request.getParameter("param1");  
  11.         //输出  
  12.         response.setContentType("text/html;charset=UTF-8");  
  13.         PrintWriter out = response.getWriter();  
  14.         out.write("你传递来的参数param1=" + param1);  
  15.         out.close();  
  16.     }  
  17. }  

Demo2.java 
Java代码   收藏代码
  1. /** 
  2.  * User: liuwentao 
  3.  * Time: 12-1-25 下午1:23 
  4.  */  
  5. public class Demo2 {  
  6.   
  7.     /** 
  8.      * 用post方法访问本地应用根据传递参数不同,返回不同结果 
  9.      * 
  10.      * @param args 
  11.      */  
  12.     public static void main(String[] args) {  
  13.         //创建默认的 HttpClient 实例  
  14.         HttpClient httpClient = new DefaultHttpClient();  
  15.   
  16.         HttpPost httpPost = new HttpPost("http://localhost:86/test1Servlet");  
  17.   
  18.         List<NameValuePair> formParams = new ArrayList<NameValuePair>();  
  19.         formParams.add(new BasicNameValuePair("param1", "刘文涛"));  
  20.         UrlEncodedFormEntity urlEncodedFormEntity;  
  21.   
  22.         try {  
  23.             urlEncodedFormEntity = new UrlEncodedFormEntity(formParams, "UTF-8");  
  24.             httpPost.setEntity(urlEncodedFormEntity);  
  25.             System.out.println("execurting request:" + httpPost.getURI());  
  26.             HttpResponse httpResponse = null;  
  27.             httpResponse = httpClient.execute(httpPost);  
  28.             HttpEntity httpEntity = httpResponse.getEntity();  
  29.             if (httpEntity != null) {  
  30.                 String content = EntityUtils.toString(httpEntity, "UTF-8");  
  31.                 System.out.println("Response content:" + content);  
  32.             }  
  33.         } catch (ClientProtocolException e) {  
  34.             e.printStackTrace();  
  35.         } catch (UnsupportedEncodingException e) {  
  36.             e.printStackTrace();  
  37.         } catch (IOException e) {  
  38.             e.printStackTrace();  
  39.         } finally {  
  40.             //关闭连接,释放资源  
  41.             httpClient.getConnectionManager().shutdown();  
  42.         }  
  43.     }  
  44. }  


结果: 

 

ESP8266开发之旅 网络篇⑨ HttpClient——ESP8266HTTPClient库的使用 1. 前言     在前面章节的博客中,博主介绍了ESP8266WiFi库 Tcp client的用法,并模拟了Http请求。但是,可以看出通过WiFiClient模拟Http请求,我们需要自己拼装Http请求协议,稍微不小心就很容易拼接错误。     那么有没有针对Http请求操作的库呢?答案肯定是有的,这就是博主本篇需要跟大家讲述的知识——ESP8266HTTPClient库。     请注意... 阅读详情

相关推荐

从扇束到平行束:等角重排算法在CT重建中的核心原理与MATLAB实践

本文深入解析了CT图像重建中等角扇束重排算法的核心原理与MATLAB实现。通过将实际采集的扇束投影数据,利用几何映射和两次关键插值,转换为适用于经典滤波反投影算法的平行束数据,打通了高效重建的关键路径。文章结合Shepp-Logan模型仿真,提供了完整的MATLAB代码实践,并分析了源-物距、采样率等关键参数对重建质量的影响。

cnn8visionary的博客 649

HttpGet.abort()/ HttpPost.abort()并不总是立即中止

地址:https://stackoverflow.com/questions/12014673/httpget-abort-httppost-abort-doesnt-always-abort-immediately I’m using Apache HttpComponents 4.2.1 and I’m having trouble getting HttpGet.abort() and H...

香槟的博客 4893

Calibre LVS错误排查实战:从Innovus到GDS的完整调试流程(附常见错误解析)

本文详细介绍了数字IC后端设计中,从Innovus工具输出数据到Calibre LVS物理验证的完整调试流程。文章系统性地讲解了LVS错误排查的方法论,包括报告解读、图形化定位以及针对器件不匹配、电源网络错误、短路开路等常见问题的深度解析与解决方案,旨在帮助工程师高效完成版图与原理图的一致性检查。

weixin_29218963的博客 778

HttpClient 的 abort(终止)程序示例

/* * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file

维京群岛 2万+

Basler相机和pylon viewer 的使用

文章目录1 线路连接2 安装和使用pylon 1 线路连接 电源线接到电源插座,网线接到电脑(电脑没有网口的话,可用转换器转到type-c接口)。 2 安装和使用pylon 参考 Basler pylon安装及对网口相机基本设置 相机和电脑以太网IP设置参考:balser相机IP设置 关于相机参数和pylon viewer的详细设置,可以参考pylon Viewer安装路径下的文档(Documentation) ...

Deephome的博客 1万+

从单应矩阵到实际距离:Apriltag测距完整实现指南

本文提供了基于Apriltag实现高精度视觉测距与姿态估计的完整指南。详细阐述了从相机标定、单应矩阵分解到欧拉角与距离计算的完整流程,并深入探讨了四个解的歧义选择、工程实践中的滤波优化与多Tag融合等关键问题,旨在帮助开发者在机器人、AR等应用中实现稳定可靠的距离检测与姿态估计。

n7o8p的博客 560

使用java7的try-resource-with语法用httpclient抓取网页并用jsoup获取网页对象

为什么80%的码农都做不了架构师?>>> ...

weixin_34037173的博客 692

【学习系列】SAP RAP 4:通过注解来完成一些常规需求

本文介绍了RAP应用中常用注解的实现方式,包括搜索帮助、文本描述绑定、联动搜索、字段控制等功能。通过@Consumption、@UI等注解可以实现搜索帮助定义、多字段联动、必输校验、默认排序和默认值设置。管理字段可通过@Semantics注解自动记录创建/修改信息,长文本使用@UI.multiLineText实现多行显示。这些注解可优化界面交互和数据管理,为后续行为定义功能开发奠定基础。文章结构清晰,针对不同需求提供了具体的实现方案和代码示例。

DeveloperMrMeng的博客 701

HttpClient使用时遇到几个隐蔽小坑分析

问题场景一: 1.在用get方法去获取数据时,一直都是好好的运行状态,突然遇到“Attempted read from closed stream” 错误。 相关方法用到的代码贴上: HttpGet httpGet = new HttpGet(url); httpClient = new DefaultHttpClient(); httpResponse

luozhuwang的专栏 6110

MFC Windows 程序设计[122]之树形下拉列表框(附源码)

MFC是微软公司提供的一个类库(class libraries),用以减少应用程序开发人员的工作量。其中包含大量Windows句柄封装类和很多Windows的内建控件和组件的封装类。有很多初学MFC的小伙伴们都觉得MFC比较难学,看书吧,看了就忘了,网上的一些教程又是写的含糊不清,故而学习起来很吃力...... 故此,本专栏亲自研究,实践,由易到难,层层深入,将调试过程中所遭所遇进行详细讲解,注意事项进行一一列举,希望能够帮助到各位初学MFC的小伙伴,避免走弯路,费时费力。.........

hnjzfwy的博客 460

【QT开发教程】使用Qt进行NFC编程

近场通信(NFC,Near Field Communication)技术广泛应用于现代设备中,例如智能手机、支付终端和门禁系统。Qt提供了一套完整的NFC模块,使开发者可以轻松地在应用程序中实现NFC功能。本文将介绍如何在Qt中进行NFC编程,包括基本概念、使用方法以及一些实际的示例。

科技改变人类,技术成就未来 1470

java abort_java - HttpGet.abort()/ HttpPost.abort()并不总是立即中止 - 堆栈内存溢出...

我正在使用Apache HttpComponents 4.2.1而我无法让HttpGet.abort()和HttpPost.abort()立即中止。 它大部分时间都可以工作,但偶尔连接会阻塞,直到超时。 我注意到这只发生在我明确设置超时值时。这是我的测试代码:public static void main(String[] args) throws Exception {for (int i ...

weixin_39731782的博客 868

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

HTTPCLIENT源码分析

HTTPCLIENT源码分析 http是应用层的协议,在传输层使用tcpip协议,可以从其源码分析,中间层就是调用socket(基于lwip协议)编程加上http协议实现,下面看源码。想看看其源码的可以从下面目录下载。 下载目录:点击这里 路径在middleware/third_party/httpclient中。 源码分析 以前已经介绍过httpclient api的使用了,此处不在赘述,本文主...

qq_38240926的博客 1119

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

httpclient源码介绍及其使用

httpclient源码介绍 前面介绍了hfs server的使用,本文使用设备系统为freertos,支持lwip协议,移植httpclient 后,从hfs server(这里本机电脑)获取文件。 httpclient主要存在两个结构体 //与建立网络连接相关,本文只看http,https省略(中间加层tls) typedef struct { int socket; ...

qq_38240926的博客 955

HttpClient详解--springboot+httpclient

Http简介 HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议

IT二叔的博客 9237

commons-httpclient 和 httpclient 区别

commons-httpclient 和 httpclient 区别 项目里的pom中,里面有这么两个包依赖。 <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>

小泽 3111

HttpClient学习

HttpClient学习

雷神乐乐的博客 1760

wechatpay-apache-httpclient vs 原生HttpClient:为什么选择这款微信支付SDK?

wechatpay-apache-httpclient是微信支付官方推出的Apache HttpClient装饰器,专为APIv3接口设计。它在原生HttpClient基础上封装了微信支付所需的签名、验签、证书管理等核心功能,让开发者无需从零构建安全支付流程,显著降低对接门槛。 ## 🚀 核心优势对比:SDK如何解决原生痛点? ### 1. 自动签名 vs 手动拼接(最复杂的安全环节) 原生

gitblog_00484的博客 670

(HttpClient技术)(HttpClient入门)HttpClient使用详细介绍

1 前言        网络自动化操作,已经是网络发展中的一个重要趋势,很多复杂繁琐的操作,手动完成,得花费大量的时间,所以,网络自动化操作,就变得越来越普遍,越来越重要。很多应用,都可以使用网络自动化工具去完成,比如批量登陆签到,批量刷票,批量注册邮箱,批量查询用户余额等,那么网络自动化,在java中,应该用什么工具去开发呢,那么首先想到的应该就是HttpClient了,它是网络自动化操作方面,使

蝴蝶效应 9476

gradle httpclient

1. compile'commons-httpclient:commons-httpclient:3.1' 2.工具类【根据需求改编】 package util; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus...

weixin_34184561的博客 1890

【HTTPClient】HttpClient框架详解

HttpClient简介 HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient 是 Apache Jakarta C

9721

浅谈httpclient

httpclient与commons-httpclient的关系,如何通过httpclient传递参数,如何通过httpclient实现https请求发送

lywybo的专栏 1799

HttpClient client = new HttpClient()的依赖

<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> Strin...

myme95的博客 1万+

HttpClient 图解说明

大家刚看这个名字一定会想问这是什么东东,在这我特意百度百科了下 HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。HttpClient是 Apach

liuchangxin1982的博客 1868
上一篇: Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读
下一篇: Scala和并发编程
fancylovejava
博客等级 码龄16年 1052粉丝 184原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值