java post xml流

本文介绍了一个实用的Java工具类,用于发送HTTP GET请求并处理响应。该工具类支持设置连接超时时间、请求头参数,并能以XML格式发送POST请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

 

写道
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();
	}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值