`
shenstone2012
  • 浏览: 18467 次
社区版块
存档分类
最新评论

ddsd

阅读更多
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.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;

/**
* 用于进行Https请求的HttpClient
* @ClassName: SSLClient
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:42:07

*/
public class SSLClient extends DefaultHttpClient {
    public SSLClient() throws Exception{
        super();
        SSLContext ctx = SSLContext.getInstance("TLS");
        X509TrustManager tm = new X509TrustManager() {
                @Override
                public void checkClientTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                @Override
                public void checkServerTrusted(X509Certificate[] chain,
                        String authType) throws CertificateException {
                }
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
        };
        ctx.init(null, new TrustManager[]{tm}, null);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = this.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 443, ssf));
    }
}
复制代码


2.创建一个利用HttpClient发送post请求的工具类



复制代码
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
/**
* 利用HttpClient进行post请求的工具类
* @ClassName: HttpClientUtil
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:43:38

*/
public class HttpClientUtil {
    @SuppressWarnings("resource")
    public static String doPost(String url,String jsonstr,String charset){
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = new SSLClient();
            httpPost = new HttpPost(url);
            httpPost.addHeader("Content-Type", "application/json");
            StringEntity se = new StringEntity(jsonstr);
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
            httpPost.setEntity(se);
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
}
复制代码


3.测试代码



public static void main(String[] args){
        String url = "https://192.168.1.101/xxx";
        String jsonStr = "{xxx}";
        String httpOrgCreateTestRtn = HttpClientUtil.doPost(url, jsonStr, "utf-8");
    }
分享到:
评论

相关推荐

    DDSD1352单相电子式电能表使用说明书-安科瑞 崔庭宇.pdf

    DDSD1352 单相电子式电能表使用说明书 DDSD1352 单相电子式电能表是一款用于计量低压网络的单相有功电能的电能表,同时可测量电压、电流、功率等电参量。该产品具有红外通讯功能,并可选配 RS485 通讯功能,方便...

    DDSD1352-KC使用说明书V1.1(远程预付费).zip

    DDSD1352-KC是一款先进的多功能电能表,主要应用于远程预付费系统中,为用户提供便捷的电力管理服务。这份"DDSD1352-KC使用说明书V1.1(远程预付费)"详细阐述了该电表的功能、安装、配置以及日常操作方法,旨在帮助...

    ddsd.java

    ddsd.java

    DDSD.rar_645_645掌机_DDSd_多费率_振中掌机

    振中掌机程序符合645规约,用的是电力97规约,支持多费率,功能齐全

    AFC-DDSD-采访

    AFC-DDSD-采访 我已经上传了四个HTML文档和一个VBA电子表格,它们代表了我在NPS期间从事的编码/问题。 每个HTML文件都包含Jupyter Notebook或R Studio创建的代码,它们是作业的最终产品。 OA3801 Project-Wren....

    DIRECTX中独占模式与窗口模式的切换

    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; ddsd.dwWidth = nWidth; ddsd.dwHeight = nHeight; ``` **3. 切换模式** 为了支持模式的动态切换,还...

    精彩编程与编程技巧-DirectX7.0使用心得(2)...

    ddsd1.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH ddsd1.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN ddsd1.lWidth = 640 ddsd1.lHeight = 480 Set objDDOffSurf = objDD.CreateSurface(ddsd1) ``` - **...

    StyDirectX

    Dim ddsd As DDSURFACEDESC2 Dim i As Long, lgCount As Long Set objDx = New DirectX7 '建立 DirectDraw 对象 Set objDraw = objDx.DirectDrawCreate("") '将DirectDraw对象支持的显示模式设置到...

    算法与数据结构---实用汇集篇

    DDSURFACEDESC ddsd1, ddsd2; ddsd1.dwSize = sizeof (DDSURFACEDESC); ddsd2.dwSize = sizeof(DDSURFACEDESC); lpDDSPic1-&gt;Lock(NULL, &ddsd1, DDLOCK_WAIT, NULL); lpDDSPic2-&gt;Lock(NULL, &ddsd2, DDLOCK_WAIT, ...

    vc++游戏编程指南

    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; // 这是离屏页面 ddsd.dwHeight = 480; // 高 ddsd.dwWidth = 640; // 宽 if (DD_OK != lpDD-&gt;...

    ddraw最简单的程序

    ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP; hr = dd7-&gt;CreateSurface(&ddsd, &primarySurface, NULL); ddsd.ddsCaps.dwCaps = DDSCAPS_...

    Directshow学习笔记(公司内部资料)

    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; // 创建一个离屏缓冲区 ddrval = lpDD-&gt;...

    DirectDraw的学习笔记.doc

    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; ddrval = lpDD-&gt;CreateSurface(&ddsd, &...

    面向对象和基于数据驱动的软件开发相结合-PPT课件.ppt

    面向对象和基于数据驱动的软件开发相结合(OO_DDSD)是一种实用的原型化开发模型,旨在结合面向对象的编程理念和数据驱动的设计思想,以提高软件开发的效率和质量。该模型强调立足实例,以具体软件系统为开发出发点...

    DirectDraw实例

    ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX; ddsd.dwBackBufferCount = 1; ddrval = lpDD-&gt;CreateSurface(&ddsd, &...

    感应式电能表转矩矢量分析

    ### 感应式电能表转矩矢量分析 #### 概述 感应式电能表因其结构简单、成本低、寿命长以及良好的负载特性,长期以来被供电和用电部门广泛采用作为电能计量工具。这类电能表的计量原理基于驱动转盘旋转的平均转矩,...

    在VC中调用DirectShow全屏播放视频

    ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; if (FAILED(pDD-&gt;CreateSurface(&ddsd, &pPrimarySurface, NULL))) return r; return S_OK; } ``` ...

    DirectX图像显示

    ZeroMemory(&ddsd, sizeof(ddsd)); ddsd.dwSize = sizeof(ddsd); ddsd.dwFlags = DDSD_CAPS; ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; hret = m_pDD-&gt;CreateSurface(&ddsd, &m_pDDSprimary_window, NULL...

Global site tag (gtag.js) - Google Analytics