常见请求头,在post请求之 前先了解一下,请求相关的基础

关于post 请求的方式比get  多了很多配置,其实大致一样,本想将get示例和post写在一起,这个博客功能有时有问题 一直在灰色的编辑框中跳不出去,只能另起一篇博客。

    private void doPost(String s) {
try {
// URl构建的是一上地址对象
URL url = new URL(UrlAddress);
// 创建一个连接
HttpURLConnection httpURLConnection =(HttpURLConnection) url.openConnection();
// 由于是post 请求需要配置参数
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
// 配置请求头
httpURLConnection.setRequestMethod("POST");
// 配置请求是否有缓存
httpURLConnection.setDefaultUseCaches(false);
HttpURLConnection.setDefaultRequestProperty("Accept-Charset","UTF-8");
HttpURLConnection.setDefaultRequestProperty("Content-Type","application/x-www-form-urlencoded");
// 配置好了尝试连接准备
httpURLConnection.connect();
DataOutputStream outputStream = new DataOutputStream(httpURLConnection.getOutputStream());
// 拼接请求参数,比如条件,性别,
String content = "set="+s;
outputStream.write(content.getBytes());
outputStream.flush();
outputStream.close();
//以下的处理和get 一样了
if(httpURLConnection.getResponseCode() == 200){
InputStream is =httpURLConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer Sbuffer = new StringBuffer();
String readLine = "";
while ((readLine = br.readLine())!=null){
Sbuffer.append(readLine);
}
is.close();
br.close();
httpURLConnection.disconnect();
Log.d("Text",Sbuffer.toString());
} } catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
}
} //dopost 方法方法结束

  用 HttpPost   封装好的组件的话比较直接,但是以下方法需要在另一线程调用,

private void doPost(String s) {
HttpPost httpPost = new HttpPost(urlAddress + method);
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("sex", s)); try {
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(httpResponse.getEntity());
Log.d("test", result);
} else {
Log.d("test","failed");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

httpURLConnection 请求发起post请求的更多相关文章

  1. openresty开发系列29--openresty中发起http请求

    openresty开发系列29--openresty中发起http请求 有些场景是需要nginx在进行请求转发 用户浏览器请求url访问到nginx服务器,但此请求业务需要再次请求其他业务:如用户请求 ...

  2. 关于java发起http请求

    我们到底能走多远系列(41) 扯淡: 好久没总结点东西了,技术上没什么总结,感觉做事空牢牢的.最近也比较疲惫. 分享些东西,造福全人类~ 主题: 1,java模拟发起一个http请求 使用HttpUR ...

  3. [Java] 两种发起POST请求方法,并接收返回的响应内容的处理方式

    1.利用apache提供的commons-httpclient-3.0.jar包 代码如下: /** * 利用HttpClient发起POST请求,并接收返回的响应内容 * * @param url ...

  4. java中两种发起POST请求,并接收返回的响应内容的方式  (转)

    http://xyz168000.blog.163.com/blog/static/21032308201162293625569/ 2.利用java自带的java.net.*包下提供的工具类 代码如 ...

  5. Ajax_02之XHR发起异步请求

    1.Ajax: AJAX:Asynchronous Javascript And Xml,异步的JS和XML: 同步请求:地址栏输入URL.链接跳转.表单提交-- 异步请求:使用Ajax发起,底层使用 ...

  6. python urllib2 发起http请求post

    使用urllib2发起post请求 def GetCsspToken(): data = json.dumps({"userName":"wenbin", &q ...

  7. libcurl发起post请求时间延迟问题。except为空即可

    最近在做团购酒店APP分享到qzone功能,使用libcurl访问qzone的分享cgi接口,酒店分享信息以POST方式传输,在测试的时候发现分享接口平均有2s的延迟,这延迟也太大了吧,于是乎问了空间 ...

  8. 发起post请求

    string postUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo"; //string req ...

  9. android4.0 HttpClient 以后不能在主线程发起网络请求

    android4.0以后不能在主线程发起网络请求,该异步网络请求. new Thread(new Runnable() { @Override public void run() { // TODO ...

  10. php 使用curl发起https请求

    今天一个同事反映,使用curl发起https请求的时候报错:“SSL certificate problem, verify that the CA cert is OK. Details: erro ...

随机推荐

  1. Linux C操作XML文件

    1 简介 介绍使用C语言操作xml文件. 使用的开源库:mxml mxml源码路径:michaelrsweet/mxml: Tiny XML library. (github.com) mxml官网: ...

  2. .NET 中使用 OpenTelemetry Traces 追踪应用程序

    上一次我们讲了 OpenTelemetry Logs.今天继续来说说 OpenTelemetry Traces. 在今天的微服务和云原生环境中,理解和监控系统的行为变得越来越重要.在当下我们实现一个功 ...

  3. knife4j/swagger救援第一现场

    1.前方来报,测试环境springboot项目无法启动,现场如下: Error starting ApplicationContext. To display the auto-configurati ...

  4. scrcpy 安卓投屏

    下载地址:https://github.com/Genymobile/scrcpy 电脑是WINDOWS的,下载WINDOWS版scrcpy:scrcpy-win64-v1.14.zip,解压到:D: ...

  5. JavaScript实现防抖节流函数

    review 防抖函数 防抖函数一般是短时间内多次触发,但是只有最后一次触发结束后的delay秒内会去执行相对应的处理函数. 相当于一个赛道里面一次只能跑一辆赛车,如果此时已经有一辆赛车在跑道里面跑, ...

  6. Spring源码——详细流程图(超详细)

    Spring源码流程图

  7. react的类组件的ts写法

    react的类组件的ts写法,声明的变量,props和state的写法 import React, { PureComponent } from 'react'; interface Iprops { ...

  8. 1024程序员节,写最棒的coding,做最靓的仔

    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` 1024程序员节,写最棒的coding,做最靓的仔 日期: ...

  9. 阿里云ecs自定义镜像并导出到OSS、并下载

    OSS是什么? 有个文章说得比较浅显清楚:什么是OSS?5分钟带你了解! - 知乎 (zhihu.com) 这里摘选核心内容: 白话文解释就是将系统所要用的文件上传到云硬盘上,该云硬盘提供了文件下载. ...

  10. 03-Python数据类型

    None类型 Python3中没有NULL,取而代之的是空类型None.空列表.空字典等. None是一个特殊的Python对象,表示无. None的类型是NoneType. 如果只想声明变量,而不想 ...