`
shendixiong
  • 浏览: 393074 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论
阅读更多

代码如下:

 

写道
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

public class HttpRequestUtils {
public static HttpResponse getHttpResponse(String url, Map<String, String> params) throws Exception{
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 2 * 60 * 100);
HttpConnectionParams.setSoTimeout(httpParameters, 2 * 60 * 100);
DefaultHttpClient client = new DefaultHttpClient(httpParameters);
HttpGet httpGet = new HttpGet(new URI(url));
for(Map.Entry<String, String> entry : params.entrySet()){
httpGet.setHeader(entry.getKey(), entry.getValue());
}
return client.execute(httpGet);
}

public static HttpURLConnection getUrlConnection(String url, int bufferSize) throws Exception{
URL target = new URL(url);
HttpURLConnection conn = (HttpURLConnection) target.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setChunkedStreamingMode(bufferSize);
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("contentType", "UTF-8");
conn.setRequestProperty("timenow", String.valueOf(System.currentTimeMillis()));
return conn;
}


}

 

	public String sendPostXml(String url, String postXml) {
		StringBuffer sb = new StringBuffer();
		BufferedReader reader = null;
		OutputStreamWriter out = null;
		try {
			HttpURLConnection con = HttpRequestUtils.getUrlConnection(url, postXml.length());
			con.setRequestProperty("Pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("Content-Type", "text/xml");
            out = new OutputStreamWriter(con
                    .getOutputStream());    
            out.write(new String(postXml.getBytes("UTF-8")));
            out.flush();
            out.close();
            //获取post端的响应的数据
            reader = new BufferedReader(new InputStreamReader(con
                    .getInputStream()));
            String lines;
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "UTF-8");
                sb.append(lines);
            }
            reader.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (out != null)
			{
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (reader != null)
			{
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return sb.toString();
	}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics