后台调用http短信接口

两种方式,第一种只能用GET方式,POST方式会报 411错误:
sContent = URLEncoder.encode(request.getParameter("sContent"),"utf-8");
String contents="您申请的《药品出口证书》已经审批完毕,请登录河北省食药监局行政审批系统,在“通知单”菜单中彩色打印。";
			path="http://192.168.5.42/BackManage/GwDuanxin.aspx?iMsgID=110&shouWenRen=11&shouWenTel="+shouWenTel+"&sContent="+contents;
			try {
				URL url=new URL(path);
				HttpURLConnection conn  =(HttpURLConnection) url.openConnection(); 
				conn.setRequestMethod("GET");
                conn.setDoOutput(true);
				conn.setDoInput(true);
            	conn.connect();
            	
	            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
				String line = in.readLine();
				System.out.println(" </p>     result:   " + line);
				int i_ret = conn.getResponseCode();
				String sRet = conn.getResponseMessage();
				System.out.println("sRet   is:   " + sRet);
				System.out.println("i_ret   is:   " + i_ret);
				return null;

			} catch (MalformedURLException e) {
				// TODO 自动生成的 catch 块
		
 
 
引用
e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }


第二种直接用POST
PostMethod method = null;
		    try {
		    HttpClient client = new HttpClient();
		    //client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");//指定传送字符集为UTF-8格式
		    client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);//设置连接超时时间为30秒(连接初始化时间)
		    method = new PostMethod(path);//带参数的那个请求的访问串
			//path="http://192.168.5.42/BackManage/GwDuanxin.aspx?iMsgID=110&shouWenRen=11&shouWenTel="+shouWenTel+"&sContent="+contents;

		    method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"gbk");//格式转换
//		    method.addParameter("iMsgID", "测试20110802");//短信内容
//		    method.addParameter("shouWenRen", "贾玉龙");//
//		    method.addParameter("shouWenTel", "18633020413");//
//		    method.addParameter("sContent", contents);//
		    		   		 		   		    		    
		    int statusCode = client.executeMethod(method);
		    String info = new String(method.getResponseBody(),"gbk");
		    System.out.println(info);
		   // log.info("访问主机开始...");
		    if (statusCode != HttpStatus.SC_OK) {
		        //失败
		    	System.out.println("发送失败");
		    }else{
		        //成功
		    	System.out.println("发送成功");
		    }
		    //log.info("访问主机结束.");
		    client.getHttpConnectionManager().closeIdleConnections(1);
		    //log.info("访问主机:" + hostaddr[i] + "完成.");
		}catch (Exception e) {
		    //log.error("无法访问主机:"+hostaddr[i]);
		}finally{
		    if(method != null)
		        method.releaseConnection();
		}
		    return null;
		}



系统中用的GBK编码格式,用以上两种方式都会乱码, 还在处理乱码中....


猜你喜欢

转载自451640893.iteye.com/blog/2306161