http connection

package com.ad.web.servlet.conf.rmapi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import cn.richinfo.cmail.common.config.PropertiesUtil;
import com.ad.web.common.CommonConst;
public class GetConfrmi
{
    private static final Logger log = Logger.getLogger(GetConfrmi.class);
    public static String doCommand(String sid,String rmkey,String from,HttpServletRequest request)
    {
        String config="";
        String requestUrl =PropertiesUtil.getProperty(CommonConst.CONF_SVRSERVER)+ "?sid=" + sid+"&from="+from;
        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(requestUrl);
        requestUrl = m.replaceAll("");
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection httpconn = (HttpURLConnection) url.openConnection();
            httpconn.setConnectTimeout(6000);
            httpconn.setReadTimeout(6000);
            httpconn.setDoInput(true);
            httpconn.setDoOutput(true);
            httpconn.setRequestMethod("GET");
            httpconn.setRequestProperty("Accept", "text/plain, */*");
            httpconn.setRequestProperty("Content-Type", "text/html; charset=UTF-8");
            String cookies = request.getHeader("Cookie");        
            httpconn.setRequestProperty("Cookie", cookies);
            InputStream inPs = httpconn.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(inPs,"UTF-8"));
            String tempLine = rd.readLine();
            StringBuffer tempStr = new StringBuffer();
            while (tempLine != null) {
                tempStr.append(tempLine);
                tempLine = rd.readLine();
            }
            config = tempStr.toString();
            //log.info("sid="+sid+" |result=0k | config="+config);
            return config;
        } catch (IOException e) {            
            log.error("sid="+sid + "| reqUrl="+requestUrl+" |result=failure | json="+config,e);
            return config;
        }
    }
}

 
posted @ 2016-11-08 09:46  albert_think  阅读(324)  评论(0编辑  收藏  举报