zoukankan      html  css  js  c++  java
  • 读流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; }
    昨日已逝正在输入···
    赞赏支持!!!
    分享到: 更多
  • 相关阅读:
    通过JDBC连接oracle数据库的十大技巧
    asp.net里导出excel表方法汇总
    流式媒体
    html 另存为/打印/刷新/查看原文件等 代码
    JSP 语法详解
    让一个网页打开的同时自动为另一个网页自动填写表单并提交
    StreamWriter输出中文乱码的问题
    “mailto”的六则应用技巧
    返回目录大小
    JDBC基础(二)
  • 原文地址:https://www.cnblogs.com/rhxuza1993/p/6007684.html
Copyright © 2011-2022 走看看