get方式调用http接口 Header赋参数值

15410年前 (2015-11-14)java技术12935

 

  /** 
     195.     * 发送 get请求 
     196.     */  
         public static void get() {  
             CloseableHttpClient httpclient = HttpClients.createDefault();  
            try {  
                 // 创建httpget.    
                 HttpGet httpget = new HttpGet("http://211.156.193.140:8002/cotrackapi/api/track/mail/1104389559880");  
                 httpget.setHeader("authenticate", "sdgszzswsgp_xiangmu123456");
                 httpget.setHeader("version", "ems_track_cn_1.0");
                 httpget.setHeader("Accept-Charset", "ISO8859-1");   
                // System.out.println("executing request " + httpget.getURI());  
                 // 执行get请求.    
                 CloseableHttpResponse response = httpclient.execute(httpget);  
                 try {  
                     // 获取响应实体    
                  HttpEntity entity = response.getEntity();  
                   //  System.out.println("--------------------------------------");  
                     // 打印响应状态    
                    /// System.out.println(response.getStatusLine());  
                    if (entity != null) {  
                        // 打印响应内容长度    
                     //  System.out.println("Response content length: " + entity.getContentLength());  
                       // 打印响应内容    
                       String jsmc=EntityUtils.toString(entity);
                        System.out.println("Response content: " + jsmc);  
                        String jsmc1=new String(jsmc.getBytes("ISO-8859-1"),"GBK");
                        System.out.println("------------------------------------"+jsmc1); 
                        String jsmc2=new String(jsmc.getBytes("ISO-8859-1"),"utf-8");
                        System.out.println("------------------------------------"+jsmc2); 
//                        String jsmc1=new String(jsmc.getBytes("ISO-8859-1"),"GBK");
//                        System.out.println("------------------------------------"+jsmc1); 
                    }  
                   
                 } finally {  
                     response.close();  
                 }  
            } catch (ClientProtocolException e) {  
                e.printStackTrace();  
            } catch (ParseException e) {  
               e.printStackTrace();  
            } catch (IOException e) {  
                e.printStackTrace();  
            } finally {  
                // 关闭连接,释放资源    
                 try {  
                    httpclient.close();  
                 } catch (IOException e) {  
                     e.printStackTrace();  
                }  
            }  
         }  

 

本文原创,转载必追究版权。

分享给朋友:

相关文章

js限制input只能输入数字、英文、汉字

 1.只能输入数字和英文的:  <input onkeyup="value=value.replace(/[\W]/g,'') "...

freemarker 判断日期变量为空处理 及InvalidReferenceException异常处理

at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:98);InvalidR...

MyEclipse 10 性能大优化

MyEclipse 10 性能大优化

 MyEclipse 10已发布,新版本的特性能够大大提高开发者的效率。但同时,由于电脑配置的原因,很多开发者在使用MyEclipse的时候,速度都不是很快,需要我们对MyEclipse进行...

freeMarker Jfinal 获取session里的值

问题:freeMaker session取值的常用格式都试过 session["xxx"],session.xxx 直接xxx 都取不出来?????解决:JFinal与Struts...

MyEclipse10.7注册码生成

注意经验里的工具可能不可用,请点击 生成注册码工具  下载。提取码:p1w4MyEclipse10.7注册码激活步骤:点击下面的链接http://jingyan.baidu.com/arti...

是谁动了我的座位

是谁动了我的座位

女孩一上火车,见自己的座位上坐着一男士。她核对自己的票,客气地说:“先生,您坐错位置了吧?” 男士拿出票嚷嚷着:“看清楚点,这是我的座,你瞎了?” 女孩仔细看了他的票,不再做声,默...

评论列表

点我收录您
10年前 (2015-11-16)

爆照呢?发点别的东西嘛,纯技术博客有点单调

萨瓦迪卡 回复:
谢谢您的建议[害羞],会加内容的[太开心]右上角 关于站长 有照片额
10年前 (2015-11-17)
就要来海淘网
就要来海淘网
9年前 (2016-10-11)

支持下,非常不错。

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。