import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
/** * @author xuza * @param params 参数 * @param url 字符串 * @return */ public static String sendChar(String params,String url){ String sTotalString = ""; try { URL newUrl = null; HttpURLConnection httpConn = null; OutputStream output = null; OutputStreamWriter outr = null; newUrl = new URL(url); httpConn = (HttpURLConnection) newUrl.openConnection(); HttpURLConnection.setFollowRedirects(true); httpConn.setDoOutput(true); httpConn.setRequestMethod("POST"); httpConn.setRequestProperty("Content-Type", "text/xml"); httpConn.connect(); output = httpConn.getOutputStream(); outr = new OutputStreamWriter(output); // 写入请求参数 outr.write(params.toString().toCharArray(), 0, params .toString().length()); outr.flush(); outr.close(); // System.out.println("send ok"); int code = httpConn.getResponseCode(); // System.out.println("code " + code); // System.out.println(httpConn.getResponseMessage()); //读取响应内容 String sCurrentLine = ""; if (code == 200) { java.io.InputStream is = httpConn.getInputStream(); BufferedReader reader = new BufferedReader( new InputStreamReader(is)); while ((sCurrentLine = reader.readLine()) != null) if (sCurrentLine.length() > 0) sTotalString = sTotalString + sCurrentLine.trim(); } else { sTotalString = "远程服务器连接失败,错误代码:" + code; } System.out.println("response:" + sTotalString); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return sTotalString; }