linux下用GPRS模块发送POST请求

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_30102889/article/details/71136522

GPRS模块发送POST请求

    相信大家只要是80、90后都知道GPRS是什么吧(啥?不记得了!)记不记得小时候偷偷用手机上网看学(you)习(xi)资料,手机最上面一个大写的’G‘在不停的闪,那个就是GPRS网络,后来又出现了E(EGPRS)、H也就是大家常说的几G网,现在发展到4G网速比当年玩小霸王手机的时候快了......不知多少倍。

    说了这么多废话,那我们到底问什么用这个GPRS通信呢(好像没有说POST请求,不知道的拿本计算机网络面壁去!),那就要说GPRS的优点了:

    1.传输速率相对较低。有人要问了,速率低还是优点?!那要看你用来干嘛了,传个几KB甚至几B的数据包用4G网简直浪费资源!

    2.入网速度快,而且网络覆盖广泛;

    3.资料内容相对较少,学习强度较低,也就是用起来方便。

AT指令

    我的项目用的是SIM900,GPRS模块都是通过向串口发送AT指令来达到各种操作目的的(不知道如何操作串口的请看我的另一篇文章),比如收发短信、接打电话、上网等等,这里只介绍如何建立TCP连接以及发送数据包。

    我这里将所有指令放到了一个指针数组中(注意和数组指针的区别),还有注意每个指令后面要加回车符("\r"),还有各种转意字符:

unsigned char *cmd[] = {
				"AT\r", 				 \     
				"AT+CCID\r", 				\
				"AT+CSQ\r", 				\
				"AT+CGCLASS=\"B\"\r", 				\
				"AT+CGDCONT=1,\"IP\",\"CMNET\"\r", 				\
				"AT+CGATT=1\r", 				\
				"AT+CIPCSGP=1,\"CMNET\"\r", 				\
				"AT+CIPSTART=\"TCP\",\"182.254.244.212\",\"80\"\r", 				\
				"AT+CIPSEND\r", 				\
				"AT+CIPCLOSE\r" 				\
                        };
    发送:AT                        //初始化

    返回:OK

    发送:AT+CCID          //获取SIM卡标识

    返回:7454645646(我乱写的)

                OK

    发送:AT+CSQ           //获取信号强度

    返回:+CSQ:24,99

                OK

    发送:AT+CSCLASS="B"   //选择移动台类别B

                AT+CGDCONT=1,"IP","CMNET"      //设置上下文

                AT+CGATT=1         //附着GPRS业务

                AT+CIPCSGP=1,"CMNET"    //设置为GPRS连接模式

                AT+CIPSTART="TCP","182.254.144.232","80"     //与特定IP地址的特定端口建立TCP连接,由于是发POST请求,所以是80端口

               AT+SEND          //开始发送数据(具体用法后面说)

               AT+CIPCLOSE

AT+SEND

    当我们与IP+端口建立好了TCP连接后,就要开始真正的目的:发送数据了。

    AT+SEND的一种用法是:

    直接AT+SEND,返回的是'>'字符,也就是等待我们向这个缓冲区写入数据,当写完之后,我们要最后写如一个16进制数据0x1A(熟悉文件结构的就能想到这个是EOF文件结束符),注意是以16进制方式!然后就可以等待返回SEND OK了

     另一种用法是:

     AT+SEND=10,这里的10是举例。返回也是'>'字符,不同的是,这个缓冲区写满10个字节时自动发送。也就是说如果我们第一次写入hello!时不够10个,于是等待继续输入,然后我们再写入world?,好了以经超过10个了自动发送,但是你实际发送出去的绝对不是hello!(换行)world?,这个大伙可以自己测试。

发送POST数据包

    为什么我特地详细说了一下AT+SEND指令呢,因为我们要发送的POST数据包中什么空格、回车、换行各种乱,所以我目的是告诉大家一定要注意你要发送的字节数,不然就会像我一样收到了好多错误码返回。

    这是我的数据包(‘*’处是我隐藏的):

unsigned char *post[] = {
				 "POST /rc/*.php/*/* HTTP/1.1\r\n", 				\
			   	 "Host: www.*.cn\r\n", 				\
			       	 "Content-Type: application/x-www-form-urlencoded\r\n", 				\
			   	 "Content-Length: *\r\n\r\n", 				\
				 "*=*&*=*&*=*"  				\
                         };
    大家也可以用别的格式发送数据包,不过最好是在服务器端抓下包,看看是不是我们想要的格式。


展开阅读全文

发送post请求问题?

10-16

有哪位大牛指导一下,这个问题困扰我半天了!我在跟其他公司的一个系统进行对接时,给对方系统发送一个post的请求rn这是我的写的方法:rntry rn URL url = new URL( "https://epcis-ptp-stg.dmzstg.pingan.com.cn:19443/epcis.ptp.partner.getAhsEPolicyPDFWithCert.do?"); rn URLConnection connection = url.openConnection(); rn connection.setDoOutput(true); rn //发送域信息 rn OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "8859_1"); rn out.write( "umCode="+umCode+"&validateCode="+validateCode+"&policyNo="+policyNo+"&isSeperated="+isSeperated+"&curTime="+curTime+"&signValue="+signValue);//这里组织域信息 rn out.flush(); rn out.close(); rn //获取返回数据 rn InputStream in = connection.getInputStream(); rnrn catch (MalformedURLException e) rn // TODO Auto-generated catch block rn e.printStackTrace(); rn catch (UnsupportedEncodingException e) rn // TODO Auto-generated catch block rn e.printStackTrace(); rn catch (IOException e) rn // TODO Auto-generated catch block rn e.printStackTrace(); rn rn调试后我发现:总在connection.getOutputStream()时出异常。说是证书方面的问题。错误提示是:rnjavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targetrn at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:150)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1476)rn at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:174)rn at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:168)rn at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:847)rn at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:106)rn at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:495)rn at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:433)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:815)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1025)rn at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1038)rn at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:402)rn at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:170)rn at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:836)rn at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:230)rn at cn.com.caissa.erp.insurance.insbill.bs.impl.InsBillBs.getSignValue(InsBillBs.java:451)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:585)rn at org.springframework.aop.framework.AopProxyUtils.invokeJoinpointUsingReflection(AopProxyUtils.java:61)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:149)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:116)rn at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:56)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:138)rn at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:152)rn at $Proxy64.getSignValue(Unknown Source)rn at cn.com.caissa.erp.insurance.insbill.action.InsBillAction.getSignValue(InsBillAction.java:330)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn at java.lang.reflect.Method.invoke(Method.java:585)rn at venus.frames.base.action.DefaultDispatchAction.service(DefaultDispatchAction.java:339)rn at venus.frames.base.action.BaseAction.execute(BaseAction.java:172)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)rn at venus.frames.base.action.DefaultServlet.process(DefaultServlet.java:191)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at gap.logOperation.util.UserInfoBuilder.doFilter(UserInfoBuilder.java:98)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at com.use.filter.SignonFilter.doFilter(SignonFilter.java:62)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at venus.frames.web.SetEncodingFilter.doFilter(SetEncodingFilter.java:33)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at com.jamonapi.JAMonFilter.doFilter(JAMonFilter.java:57)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)rn at java.lang.Thread.run(Thread.java:595)rnCaused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targetrn at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:221)rn at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:145)rn at sun.security.validator.Validator.validate(Validator.java:203)rn at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:172)rn at com.sun.net.ssl.internal.ssl.JsseX509TrustManager.checkServerTrusted(SSLContextImpl.java:320)rn at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:840)rn ... 60 morernCaused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targetrn at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:236)rn at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:194)rn at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:216)rn ... 65 morern2012-10-16 11:56:58 org.apache.catalina.core.ApplicationDispatcher invokern严重: Servlet.service() for servlet jsp threw exceptionrn哪位大牛指点。。。谢了... 论坛

没有更多推荐了,返回首页