<commit_before>package com.example.FrameWorkCollection.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpStatus;

import android.location.Address;

/**
 * HttpClientѾʱ
 */
public class HttpUrlConnectionUtil {
	/**
	 * HttpURLConnection:GETĻд
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public static String get(String url) throws IOException {
		HttpURLConnection connection = (HttpURLConnection) new URL(url)
				.openConnection();
		connection.setRequestMethod("GET");
		connection.setConnectTimeout(15 * 3000);
		connection.setReadTimeout(15 * 3000);

		int status = connection.getResponseCode();
		if (status == HttpStatus.SC_OK) {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			InputStream is = connection.getInputStream();
			byte[] buffer = new byte[2048];
			int len;
			while ((len = is.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			is.close();
			out.flush();
			out.close();
			return new String(out.toByteArray());
		}
		return null;
	}

	/**
	 * HttpURLConnection:POSTĻд
	 * 
	 * @param url
	 * @param content
	 *            ﴫݲֵʽ
	 * @return
	 * @throws IOException
	 */
	public static String post(String url, String content) throws IOException {
		HttpURLConnection connection = (HttpURLConnection) new URL(url)
				.openConnection();
		connection.setRequestMethod("POST");
		connection.setConnectTimeout(15 * 3000);
		connection.setReadTimeout(15 * 3000);
		connection.setDoOutput(true);

		// ΪPOSTGETĲ֮ͬ
		// ǵüһͷheader
		connection.addRequestProperty("content-type", "application/json");
		// contentݴĲ裨崫Σ
		OutputStream os = connection.getOutputStream();
		os.write(content.getBytes());

		int status = connection.getResponseCode();
		if (status == HttpStatus.SC_OK) {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			InputStream is = connection.getInputStream();
			byte[] buffer = new byte[2048];
			int len;
			while ((len = is.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			is.close();
			out.flush();
			out.close();
			return new String(out.toByteArray());
		}

		return content;

	}

}<commit_msg>因为GET/POST可能要增加多个请求头，所以将多个请求头封装在Map<String, String>headers，通过 for (Map.Entry<String, String> entry : headers.entrySet()) { 			connection.addRequestProperty(entry.getKey(), entry.getValue()); 		} 来方便地增加请求头<commit_after>package com.example.FrameWorkCollection.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import org.apache.http.HttpStatus;

import android.location.Address;

/**
 * HttpClientѾʱ
 */
public class HttpUrlConnectionUtil {
	/**
	 * HttpURLConnection:GETĻд
	 * 
	 * @param url
	 * @param headers
	 *            Ҫӵͷheadersͷmap
	 * @return
	 * @throws IOException
	 */
	public static String get(String url,Map<String, String>headers) throws IOException {
		HttpURLConnection connection = (HttpURLConnection) new URL(url)
				.openConnection();
		connection.setRequestMethod("GET");
		connection.setConnectTimeout(15 * 3000);
		connection.setReadTimeout(15 * 3000);
		
		addHeader(headers, connection)

		int status = connection.getResponseCode();
		if (status == HttpStatus.SC_OK) {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			InputStream is = connection.getInputStream();
			byte[] buffer = new byte[2048];
			int len;
			while ((len = is.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			is.close();
			out.flush();
			out.close();
			return new String(out.toByteArray());
		}
		return null;
	}

	/**
	 * HttpURLConnection:POSTĻд
	 * 
	 * @param url
	 * @param content
	 *            ﴫݲֵʽ
	 * @param headers
	 *            Ҫӵͷheadersͷmap
	 * @return
	 * @throws IOException
	 */
	public static String post(String url, String content,
			Map<String, String> headers) throws IOException {
		HttpURLConnection connection = (HttpURLConnection) new URL(url)
				.openConnection();
		connection.setRequestMethod("POST");
		connection.setConnectTimeout(15 * 3000);
		connection.setReadTimeout(15 * 3000);
		connection.setDoOutput(true);

		// ǵüһͷheader
		// connection.addRequestProperty("content-type", "application/json");
		// ͷкܶ࣬ڷﴫһmapֵͷ
		addHeader(headers, connection);
		// ΪPOSTGETĲ֮ͬ
		// contentݴĲ裨崫Σ
		OutputStream os = connection.getOutputStream();
		os.write(content.getBytes());

		int status = connection.getResponseCode();
		if (status == HttpStatus.SC_OK) {
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			InputStream is = connection.getInputStream();
			byte[] buffer = new byte[2048];
			int len;
			while ((len = is.read(buffer)) != -1) {
				out.write(buffer, 0, len);
			}
			is.close();
			out.flush();
			out.close();
			return new String(out.toByteArray());
		}

		return content;

	}

	/**
	 * ͷ
	 * 
	 * @param headers
	 * @param connection
	 */

	private static void addHeader(Map<String, String> headers,
			HttpURLConnection connection) {
		if (headers==null||headers.size()=0) {
			return;
		}
		for (Map.Entry<String, String> entry : headers.entrySet()) {
			connection.addRequestProperty(entry.getKey(), entry.getValue());
		}
	}
}
{'fraction_non_alphanumeric': 0.13221464134243038, 'fraction_numerical': 0.013461183846579385, 'mean_word_length': 3.9534246575342467, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6961393', 'n_tokens_mistral': 1998, 'n_tokens_neox': 1731, 'n_words': 359}