• 读流testDemo



    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; }
    昨日已逝正在输入···
    赞赏支持!!!
    分享到: 更多
  • 相关阅读:
    private知识笔记
    finalize知识笔记
    java实现队列的练习
    测试知识笔记(2)
    static和final知识笔记
    测试知识笔记(1)
    overloading知识笔记
    windows Copssh + git 搭建git服务器
    Java Servlet规范
    身份证验证JS代码
  • 原文地址:https://www.cnblogs.com/rhxuza1993/p/6007684.html
Copyright © 2020-2023  润新知