`
心动音符
  • 浏览: 329441 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

4种调用https服务的方式

阅读更多
以前在网厅的时候,请求计费的账单开始使用的是httpclient,后来因为性能问题,换成了使用socket 发送http请求的方式,由于计费服务器端使用了redirect ,因此还需要从响应报文中得到Location 中的url,再次发送一次http请求才能完成整个业务逻辑!

    Https 资源在访问的时候会让你输入用户名和密码


   1.使用linux 的curl命令

    [root@xhuvm03 ~]# curl -k --basic --user "tcloudadmin:tcloud123" --data "" --header 'Content-Type: application/xml' https://123.124.189.***/api/account.list
<Users>  
  <User>  
    <isAdmin>1</isAdmin>  
    <id>f1ebe39d-d0b6-4292-b3cd-774bf945bf63</id>  
    <name>tcloudadmin</name>  
    <groupId>be2e0f3a-7684-4b8e-b04d-6ee75aa3d099</groupId>  
  </User>  
  <User>  
    <isAdmin>1</isAdmin>  
    <id>653d60c5-dc7b-488a-a861-1c67873057fd</id>  
    <name>gaoyang</name>  
    <groupId>be2e0f3a-7684-4b8e-b04d-6ee75aa3d099</groupId>  
  </User>  
  <User>  
    <isAdmin>1</isAdmin>  
    <id>2d393438-9c8f-4704-8dfd-9f00fb7d7d18</id>  
    <name>teststorage</name>  
    <groupId>be2e0f3a-7684-4b8e-b04d-6ee75aa3d099</groupId>  
  </User>  
</Users>  
[root@xhuvm03 ~]#   

-k/--insecure      Allow connections to SSL sites without certs (H)
--basic         Use HTTP Basic Authentication (H)
-u/--user <user[:password]> Set server user and password
-d/--data <data>   HTTP POST data (H)
-H/--header <line> Custom header to pass to server (H)




方式2: 使用socket发送http请求字符串到https 服务上


curl -k --basic --user "tcloudadmin:tcloud123" --data "" --header 'Content-Type: application/xml' https://123.124.189.***/api/account.list -v

使用-v 选项可以 看到http请求的过程和内容,可以作为我们拼 Http 请求字符串的依据

[root@xlhu-linux ~]# curl -k --basic --user "tcloudadmin:tcloud123" --data "" --header 'Content-Type: application/xml' https://123.124.189.196/api/account.list -v  
* About to connect() to 123.124.189.xxx port 443  
*   Trying 123.124.189.xxx... connected  
* Connected to 123.124.189.xxx (123.124.189.xxx) port 443  
* successfully set certificate verify locations:  
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt  
  CApath: none  
* SSLv2, Client hello (1):  
SSLv3, TLS handshake, Server hello (2):  
SSLv3, TLS handshake, CERT (11):  
SSLv3, TLS handshake, Server key exchange (12):  
SSLv3, TLS handshake, Server finished (14):  
SSLv3, TLS handshake, Client key exchange (16):  
SSLv3, TLS change cipher, Client hello (1):  
SSLv3, TLS handshake, Finished (20):  
SSLv3, TLS change cipher, Client hello (1):  
SSLv3, TLS handshake, Finished (20):  
SSL connection using DHE-RSA-AES256-SHA  
* Server certificate:  
*        subject: /C=TW/ST=Taipei City/L=Taipei/O=Trend Micro/OU=CloudLego/CN=CloudLego  
*        start date: 2010-02-03 02:13:59 GMT  
*        expire date: 2020-02-01 02:13:59 GMT  
*        common name: CloudLego (does not match '123.124.189.xxx')  
*        issuer: /C=TW/ST=Taipei City/L=Taipei/O=Trend Micro/OU=CloudLego/CN=CloudLego  
* SSL certificate verify result: self signed certificate (18), continuing anyway.  
* Server auth using Basic with user 'tcloudadmin'  
> POST /api/account.list HTTP/1.1  
> Authorization: Basic dGNsb3VkYWRtaW46dGNsb3VkMTIz  
> User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5  
> Host: 123.124.189.xxx  
> Accept: */*  
> Content-Type: application/xml  
> Content-Length: 0  
>   
< HTTP/1.1 200 OK  
< Date: Sun, 16 Jan 2011 11:40:25 GMT  
< Server: Apache/2.2.15 (Fedora)  
< Vary: Authorization  
< Content-Type: text/xml; charset=utf-8  
< Connection: close  
< Transfer-Encoding: chunked  
<Users>  
  <User>  
    <isAdmin>1</isAdmin>  
    <id>f1ebe39d-d0b6-4292-b3cd-774bf945bf63</id>  
    <name>tcloudadmin</name>  
    <groupId>be2e0f3a-7684-4b8e-b04d-6ee75aa3d099</groupId>  
  </User>  
  <User>  
    <isAdmin>1</isAdmin>  
    <id>653d60c5-dc7b-488a-a861-1c67873057fd</id>  
    <name>gaoyang</name>  
    <groupId>be2e0f3a-7684-4b8e-b04d-6ee75aa3d099</groupId>  
  </User>  
  <User>  
    <isAdmin>0</isAdmin>  
    <id>2a228769-3b2d-4d26-b2c8-697c86f78b65</id>  
    <name>test1234</name>  
    <groupId>ce2666d0-6c95-47f3-a908-cadf333a214e</groupId>  
  </User>  
  <User>  
    <isAdmin>1</isAdmin>  
    <id>543ae799-df16-438d-9071-6618f5c09ba3</id>  
    <name>test123</name>  
    <groupId>be2e0f3a-7684-4b8e-b04d-6ee75aa3d099</groupId>  
  </User>  
  <User>  
    <isAdmin>0</isAdmin>  
    <id>3bbc0f32-4490-49fd-8944-751ae28c1073</id>  
    <name>elaster-demo</name>  
    <groupId>fb86fe86-210a-4242-b157-fce26ba41545</groupId>  
  </User>  
  <User>  
    <isAdmin>0</isAdmin>  
    <id>bd2636fa-b92e-47e8-b658-e540e9307839</id>  
    <name>test11</name>  
    <groupId>a2b2179a-8f96-420a-be59-1cc5d12394e3</groupId>  
  </User>  
  <User>  
    <isAdmin>0</isAdmin>  
    <id>4867561a-9566-4740-a3ce-4bf78289490c</id>  
    <name>gavin</name>  
    <groupId>237b3b70-5d13-4325-a5aa-83a28aabb693</groupId>  
  </User>  
</Users>  
* Closing connection #0  
* SSLv3, TLS alert, Client hello (1):  
[root@xlhu-linux ~]#   


这种方式调用要求 掌握Http协议的请求 格式,如果请求协议的格式错误,则不能得到正确的返回结果!
必须要知道的:

a.
报文都由5个成员组成,其中请求报文的结构如下:
1、第1成员:请求行(Request-Line)或状态行(Status-line)
2、第2成员:通用头(General-Header)
3、第2成员:请求头(Request-Header)
4、第4成员:实体头(Entity-Header)
5、第5成员:实体主体(Entiry-Body)

b. 每个请求行都要以 回车换行结尾
c. 协议结束的标志是 2个换行
c. 如果返回的 响应有 location 字段,也就是重定向了 需要我们根据location 字段重新发起请求


下面是 一个socket 拼接 http请求字符串的 例子
import java.io.BufferedReader;  
import java.io.BufferedWriter;  
import java.io.InputStreamReader;  
import java.io.OutputStreamWriter;  
import java.io.PrintWriter;  
import java.net.Socket;  
  
import javax.net.ssl.SSLSocketFactory;  
  
  
public class SocketHttpsClient  
{  
    public static void main(String[] args)  throws Exception  
    {  
        String url="https://123.124.189.xxx:443/api/vm.list";  
        SSLSocketFactory ssf = (SSLSocketFactory) SSLSocketFactory.getDefault();  
        String reqMsg=getFirestRequestMsg(url);  
        Socket socket=null;  
        try {  
            System.out.println("请求消息:"+reqMsg);  
             socket = ssf.createSocket("123.124.189.xxx", 443);     
              
            PrintWriter tOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));  
              tOut.write(reqMsg);  
              tOut.println();  
              tOut.flush();  
              System.out.println("消息发送成功!等待返回结果。。。");  
  
              BufferedReader tIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
              String tLine = null;  
              StringBuilder sb=new StringBuilder();  
              while ((tLine = tIn.readLine()) != null) {  
                sb.append(tLine).append("\n");  
              }  
              System.out.println("返回内容:"+sb.toString());  
            
        } catch (Exception e) {  
            System.out.println(e);  
            throw e;  
        }finally{  
            if(socket!=null){  
                socket.close();  
            }  
        }  
    }  
    private static String getFirestRequestMsg(String url){  
        StringBuilder reqMsg = new StringBuilder("")  
        .append("POST ").append("/api/vm.list").append(" HTTP/1.1").append("\r\n")  
        .append("Authorization: Basic dGNsb3VkYWRtaW46dGNsb3VkMTIz").append("\r\n")  
        .append("User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5").append("\r\n")  
        .append("Host: ").append("123.124.189.xxx").append("\r\n")  
        .append("Accept: */*").append("\r\n")  
        .append("Content-Type: application/xml").append("\r\n")  
        .append("Content-Length: 0").append("\r\n")  
        .append("\r\n")  
        .append("\r\n");  
        return reqMsg.toString();  
    }  
      
}  

3. 使用HttpClient,首先直接访问 https://123.124.189.xxx/api/account.list ,使用firefox 的firebug插件拦截http请求和响应

package cn.com.xinli.test.httpclient;  
import java.io.InputStream;  
  
import org.apache.commons.httpclient.HttpClient;  
import org.apache.commons.httpclient.NameValuePair;  
import org.apache.commons.httpclient.methods.PostMethod;  
  
   
  
public class TestHttps {  
  
   
/** 
 * 参考 
 * http://wanglei0119.iteye.com/blog/607046 
 */  
    /** 
 
     * @param args 
 
     */  
  
    public static void main(String[] args) {  
      
        HttpClient httpclient = new HttpClient();      
        PostMethod postMethod = new PostMethod("https://123.124.189.xxx:443/api/vm.list");  
        NameValuePair[] data = {};  
  
       try {  
  
           postMethod.addRequestHeader("Content-Type","application/xml");  
  
           postMethod.addRequestHeader("Authorization", "basic dGNsb3VkYWRtaW46dGNsb3VkMTIz");  
  
           postMethod.setRequestBody(data);  
     
           httpclient.executeMethod(postMethod);  
           InputStream insr = postMethod.getResponseBodyAsStream();  
           int respInt = insr.read();  
  
            while (respInt != -1) {  
  
                 System.out.print((char) respInt);  
  
                 respInt = insr.read();  
            }  
  
       } catch (Exception e) {  
  
           System.out.println(e.getLocalizedMessage());  
  
       } finally {  
  
           postMethod.releaseConnection();  
  
       }  
    }  
}  

运行这段代码 会报一个错误 unable to find valid certification path to requested target

这个文章有解决方案 :

http://wanglei0119.iteye.com/blog/607046


当使用 正确生成的cert 后 httpclient 可以调用到https的服务了


方式4 :还是使用httpclient ,使用 httpclient的 X509TrustManager 类,这种调用方式不需要客户端制作证书,很方便!
package com.platform.vmo.elasterAgent.elaster;  
import java.io.InputStreamReader;  
import java.security.cert.CertificateException;  
import java.security.cert.X509Certificate;  
import javax.net.ssl.SSLContext;  
import javax.net.ssl.TrustManager;  
import javax.net.ssl.X509TrustManager;  
import org.apache.http.HttpEntity;  
import org.apache.http.HttpResponse;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.conn.scheme.Scheme;  
import org.apache.http.conn.ssl.SSLSocketFactory;  
import org.apache.http.entity.StringEntity;  
import org.apache.http.impl.client.DefaultHttpClient;  
  
public class HttpClinetTest {  
    public static void main(String[] args) throws Exception{  
        // First create a trust manager that won't care.  
        X509TrustManager trustManager = new X509TrustManager() {  
            public void checkClientTrusted(X509Certificate[] chain,  
                    String authType) throws CertificateException {  
                // Don't do anything.  
            }  
  
            public void checkServerTrusted(X509Certificate[] chain,  
                    String authType) throws CertificateException {  
                // Don't do anything.  
            }  
  
            public X509Certificate[] getAcceptedIssuers() {  
                // Don't do anything.  
                return null;  
            }  
  
        };  
        // Now put the trust manager into an SSLContext.  
        SSLContext sslcontext = SSLContext.getInstance("SSL");  
        sslcontext.init(null, new TrustManager[] { trustManager }, null);  
  
        // Use the above SSLContext to create your socket factory  
        // (I found trying to extend the factory a bit difficult due to a  
        // call to createSocket with no arguments, a method which doesn't  
        // exist anywhere I can find, but hey-ho).  
        SSLSocketFactory sf = new SSLSocketFactory(sslcontext);  
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
  
        DefaultHttpClient httpclient = new DefaultHttpClient();  
        httpclient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", sf, 443));  
          
          
        String requset ="https://180.168.35.140/api/vm.list";  
        HttpPost httpPost = new HttpPost(requset);  
        String result = "";  
        // Execute HTTP request  
        httpPost.setHeader("Authorization", "basic " + "dGNsb3VkYWRtaW46dGNsb3VkMTIz");   
        httpPost.setHeader("Content-type", "application/xml");  
  
            StringEntity reqEntity;  
          
            reqEntity = new StringEntity("");  
            httpPost.setEntity(reqEntity);  
            HttpResponse response = httpclient.execute(httpPost);  
            HttpEntity resEntity = response.getEntity();  
            InputStreamReader reader = new InputStreamReader(resEntity.getContent());  
  
            char[] buff = new char[1024];  
            int length = 0;  
            while ((length = reader.read(buff)) != -1) {  
                result += new String(buff, 0, length);  
            }  
            httpclient.getConnectionManager().shutdown();  
              
            System.out.println(">>>:"+result);  
          
          
          
          
    }  
}  
分享到:
评论

相关推荐

    KEPServerEX设置_loT Gateway 调用MES的web接口服务配置方法.docx

    Kepware以插件的形式提供了4种网关: - MQTT客户端 - REST客户端 - REST服务器 - ThingWorx网关 前3种都可以很方便地配置使用。 MQTT即Message Queuing Telemetry Transport,是一种专门用于远程窄带网络的消息队列...

    基于datax实现data以rpc的方式传递json配置调用推数服务.zip

    基于datax实现data以rpc的方式传递json配置调用推数服务.zip 针对datax进行2次开发,实现data 以rpc的方式传递json配置调用推数服务,同时修复datax多处bug。项目中也引入nacos作为服务的配置中心和注册中心; 同时...

    android中跨进程通讯的4种方式

    这4种方式正好对应于android系统中4种应用程序组件:Activity、Content Provider、Broadcast和Service。其中Activity可以跨进程调用其他应用程序的Activity;Content Provider可以跨进程访问其他应用程序中的数据...

    远程过程调用RPC生成工具

    RPC可以隐藏网络通信的细节,使得开发者能够以一种类似于本地调用的方式来编写分布式应用程序。RPC广泛应用于分布式系统、微服务架构和客户端-服务器模型中。 RPC的一个关键特性是它支持多种数据类型和复杂的数据...

    java调用短信猫

    至于如何查找自己的jre,我用的是eclipse,所以只说我的方法,直接鼠标右键eclipse快捷方式—属性—快捷方式—目标栏中就有你的eclipse版本所用到的jre地址,根据这个地址可以直接将上述文件放到里面就行。...

    中断服务程序设计实验

    如果采用软件中断,即利用执行int n指令的方式执行中断服务程序,则可从系统预留给用户的中断类型号60H7FH、F1HFFH中选择一个。 2、 将中断服务程序的入口地址置入中断向量表的相应的四个存储单元中 确定了...

    4GL与web网站数据通信

    4GL与Web网站之间的通信有多种方式,即可以有MCF\WebService等方式,但这种方式比较简便,容易为开必人员所掌握,而且对于报表展现方式比较灵活,相对于CR报表来说,是比较好的一个替代方案。

    客户端获取服务端webservice接口内容的六种方式(含sm3,sm4加密解密方式)

    WebService就是一个应用程序,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web调用来实现...Web Service客户端获取服务端webservice接口内容的六种方式(含sm3,sm4加密解密方式)

    Android AIDL实现与服务相互调用方式

    以上三种类型都不需要导入(import) 4. AIDL 自动生成的接口 需要导入(import) 5. 实现android.os.Parcelable 接口的类. 需要导入(import)。 问题1 在传递非基础数据时 在参数前需加修饰符 void getDatas(in byte...

    0行代码写服务服务-服务发布-调用

    通过代码生成器,一键生成增、删、改、查代码,分为有代码和无代码两种方式。有代码方式可以在需要业务封装时直接调用生成的代码进行业务组装。有代码方式和无代码方式都可以直接调用访问。 2.可指定单表生成接口 ...

    C#中调用Servlet示例

    通用消息接口使用servlet作为服务器端服务接口,第三方应用程序通过http post的方式调用servlet,实现与通用消息接口的调用连接。 参数说明如下: msgTitle:消息标题,描述发送消息的标题 serviceId:服务编号,...

    全网最易部署,响应速度最快的ChatGPT环境 PHP版调用OpenAI接口进行问答和画图,采用Stream流模式通信

    支持GPT-3.5-Turbo和GPT-4等各种模型(后者需要修改下默认model名称)。 支持Markdown格式文本显示,如表格、代码块。对代码进行了着色,提供了代码复制按钮,支持公式显示。 支持多行输入,文本框高度自动调节,...

    RMI远程方法调用

    2、 客户端有多种时间显示方式,如模拟时钟、数字时钟。 1.2 作业要求 下面是设计时需要满足的四个要求: 1、 打破客户端和服务器开发过程中的紧耦合。 2、 打破客户端和服务器运行时的紧耦合,即不需要固定的...

    机房服务器设备维保服务方案.doc

    针对此次维保服务,内容主要有: 1 硬件保修服务 本服务是对续保客户提供的一种保障性增值服务,即对维保服务期内的产品硬件在产 品正常使用过程中可能发生的故障(人为不当操作、设备运行环境、不可抗力因素等造成 ...

    Linux 下 v4l2 和 Qt 连接usb摄像头

    源码注释很多很清晰哦!!! 使用Qt和V4L2连接USB摄像头,采集图片的方式为V4L2_PIX_FMT_MJPEG,也可修改代码切换到V4L2_PIX_FMT_YUYV,带yuyv转rgb888函数。亲测可用哦。

    html页面通过ActiveX控件调用摄像头实现拍照上传demo代码下载

    /object&gt;的方式引入该控件。完整的引用控件的示例如下: 1 &lt;object classid="clsid:1122dfdf-5fds6-4fds-8fds2-947fdsfdsfds8" 2 3 id="Camer" codebase=...

    机房服务器设备维保服务方案(1).doc

    针对此次维保服务,内容主要有: 1 硬件保修服务 本服务是对续保客户提供的一种保障性增值服务,即对维保服务期内的产品硬件在产 品正常使用过程中可能发生的故障(人为不当操作、设备运行环境、不可抗力因素等造成 ...

    服务计算与大数据.pdf

    Service Description:服务描述层:为调用服务提供了具体的方法。包含服务 的接口和实现细节。 XML Messaging:XML 信息层:用于调用服务时传送信息。 Network: 网络传输层:采用广泛使用的协议传输消息,并且能够...

    DataXServer:为DataX(https:github.comalibabaDataX)提供远程多语言调用(ThriftServer,HttpServer)分布式运行(YARN上的DataX)功能

    DataX服务器 为提供远程调用(Thrift服务器,Http服务器)...4.完善文档示例 部署 下载发布包并解压进入0.0.1目录 启动节俭服务器 ./bin/startThriftServer.sh 使用NodeJS提交测试任务到目标服务器  cd example/no

    SOA与Java用Java技术实现面向服务

    SOA与Java 用Java技术实现面向服务 资源目录:序作者及贡献者简介第1章 概述 .11.1 关于本书 .11.1.1 本书目标 11.1.2 本书面向的读者 .11.1.3 本书特征 21.2 必要阅读 .21.3 本书结构 .21.4 本书使用原则和模式的...

Global site tag (gtag.js) - Google Analytics