post 发送方式


/**

* post 方式 解码

*/

public static String getWebContentByPost(String urlString, String data,

final String charset, int timeout) throws IOException {

if (urlString == null || urlString.length() == 0) {

return null;

}

urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString : ("http://" + urlString).intern();

URL url = new URL(urlString);

System.out.println("url=="+url);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 设置是否向connection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

// Post 请求不能使用缓存

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset="+charset+"");

// 增加报头,模拟浏览器,防止屏蔽

connection.setRequestProperty("User-Agent",

"Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");

// 只接受text/html类型,当然也可以接受图片,pdf,*/*任意

connection.setRequestProperty("Accept", "*/*");// text/html

connection.setConnectTimeout(timeout);

connection.connect();

DataOutputStream out = new DataOutputStream(connection

.getOutputStream());

byte[] content = data.getBytes(charset);// +URLEncoder.encode("中文 ",

out.write(content);

out.flush();

out.close();

try {

// 必须写在发送数据的后面

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

return null;

}

} catch (IOException e) {

e.printStackTrace();

return null;

}

BufferedReader reader = new BufferedReader(new InputStreamReader(

connection.getInputStream(), charset));

String line;

StringBuffer sb = new StringBuffer();

while ((line = reader.readLine()) != null) {

sb.append(line).append("\r\n");

}

if (reader != null) {

reader.close();

}

if (connection != null) {

connection.disconnect();

}

System.out.println(sb.toString());

return URLDecoder.decode(sb.toString(), "utf-8");

}

public static String getUndecodeByPost(String urlString, String data,

final String charset, int timeout) throws IOException {

if (urlString == null || urlString.length() == 0) {

return null;

}

urlString = (urlString.startsWith("http://") || urlString.startsWith("https://")) ? urlString : ("http://" + urlString).intern();

URL url = new URL(urlString);

System.out.println("url=="+url);

HttpURLConnection connection = (HttpURLConnection) url.openConnection();

// 设置是否向connection输出,因为这个是post请求,参数要放在 http正文内,因此需要设为true

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

// Post 请求不能使用缓存

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset="+charset+"");

// 增加报头,模拟浏览器,防止屏蔽

connection.setRequestProperty("User-Agent",

"Mozilla/4.0 (compatible; MSIE 8.0; Windows vista)");

// 只接受text/html类型,当然也可以接受图片,pdf,*/*任意

connection.setRequestProperty("Accept", "*/*");// text/html

connection.setRequestProperty("Content-Length", "*/*");// text/html

connection.setConnectTimeout(timeout);

connection.connect();

DataOutputStream out = new DataOutputStream(connection

.getOutputStream());

byte[] content = data.getBytes(charset);// +URLEncoder.encode("中文 ",

out.write(content);

out.flush();

out.close();

try {

// 必须写在发送数据的后面

if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {

return null;

}

} catch (IOException e) {

e.printStackTrace();

return null;

}

BufferedReader reader = new BufferedReader(new InputStreamReader(

connection.getInputStream(), charset));

String line;

StringBuffer sb = new StringBuffer();

while ((line = reader.readLine()) != null) {

sb.append(line).append("\r\n");

}

if (reader != null) {

reader.close();

}

if (connection != null) {

connection.disconnect();

}

return sb.toString();

}

优质内容筛选与推荐>>
1、回归测试的想法
2、调用系统safair 在模拟器下无法成功
3、idea中不重启服务器更改代码(使用jrebel)
4、HTML语义化(2016/3/16更新)
5、计算机的本质


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号