Java发送https请求 post请求,请求报文形式区别

存在问题:请求时接口返回参数缺失,或者格式错误,反正就是各种明明正确的错
解决方案:正常浏览器地址栏请求的都是get请求,正常post请求的都是以表单的形式,但是也有以json的格式传输,虽说在接口文档里会说明,但是容易被忽略,在此将三种请求方式记录下。

第一种:get请求,复制直接能用
protected String get(String url){
String result = null;
HttpClient httpClient = null;
try {
httpClient = new SSLClient();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader(“Content-Type”, “application/x-www-form-urlencoded”);
httpGet.setHeader(“Accept-Charset”, “utf-8”);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity,“GBK”);
EntityUtils.consume(httpEntity);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
httpClient.getConnectionManager().shutdown();
}
return result;
}
下图为以上代码:(URL例子:http://localhost:8080//face/nt/smallprogramcontroler/testPay.jhtml?a=1&b=2)在这里插入图片描述

第二种请求:post表单格式请求(和第三种的主要区别标明了)
protected String postbd(String url, Map<String, String> parameterMap) {
//System.out.println(“87提速开始+++++++++++++”+CommonUtils.getDateStr());
Assert.hasText(url);
String result = null;
HttpClient httpClient = null;
try {
httpClient = new SSLClient();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpParams params = httpClient.getParams();
//设置请求超时5秒钟 根据业务调整
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5 * 1000);
//设置等待数据超时时间10秒钟 根据业务调整
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);
//在提交请求之前 测试连接是否可用
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
try {
// System.out.println(“post=====”+JsonUtils.toJson(parameterMap));
HttpPost httpPost = new HttpPost(url);
// httpPost.setEntity(new StringEntity(JsonUtils.toJson(parameterMap),“UTF-8”));

**

List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
        for(String key:parameterMap.keySet()){
            list.add(new BasicNameValuePair(key,parameterMap.get(key)));
        }

**
UrlEncodedFormEntity uefe = new UrlEncodedFormEntity(list,“utf-8”);
httpPost.setEntity(uefe);
System.out.println(“POST请求 JsonUtils.toJson(parameterMap)”+list.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
} catch (Exception e) {
// System.out.println(“87提速异常+++++++++++++”+CommonUtils.getDateStr());
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
// System.out.println(“result=======”+result);
return result;
}
下图为需要传入的参数map:(地址就是正常的请求不带参数地址)
在这里插入图片描述

第三种 用的比较少不多见 json格式请求(请求map同上 两个的区别加粗了)
protected String post(String url, Map<String, String> parameterMap) {
//System.out.println(“87提速开始+++++++++++++”+CommonUtils.getDateStr());
Assert.hasText(url);
String result = null;
HttpClient httpClient = null;
try {
httpClient = new SSLClient();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//==================================================
HttpParams params = httpClient.getParams();
//设置请求超时5秒钟 根据业务调整
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5 * 1000);
//设置等待数据超时时间10秒钟 根据业务调整
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 10 * 1000);
//在提交请求之前 测试连接是否可用
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, true);
//==================================================

    try {

// System.out.println(“post=====”+JsonUtils.toJson(parameterMap));
HttpPost httpPost = new HttpPost(url);
*httpPost.setEntity(new StringEntity(JsonUtils.toJson(parameterMap),"UTF-8"));*
System.out.println(“POST请求 JsonUtils.toJson(parameterMap)”+JsonUtils.toJson(parameterMap));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
} catch (Exception e) {
// System.out.println(“87提速异常+++++++++++++”+CommonUtils.getDateStr());
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
// System.out.println(“result=======”+result);
return result;
}
记录一下,以后回头看。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java发送HTTP POST请求并设置报文头的expect参数,并且请求体的格式是XML,可以使用Java内置的HttpURLConnection类。以下是一个示例代码: ```java import java.net.HttpURLConnection; import java.net.URL; import java.io.OutputStream; import java.io.BufferedReader; import java.io.InputStreamReader; public class HttpPostWithXmlAndExpectHeader { public static void main(String[] args) throws Exception { String url = "http://example.com/api"; String xmlBody = "<user><name>John</name><age>30</age></user>"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置HTTP请求方法为POST con.setRequestMethod("POST"); // 设置报文头的expect参数 con.setRequestProperty("Expect", "100-continue"); // 设置请求体格式为XML con.setRequestProperty("Content-Type", "application/xml"); // 设置请求体 con.setDoOutput(true); OutputStream os = con.getOutputStream(); os.write(xmlBody.getBytes()); os.flush(); os.close(); // 获取响应 int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 输出响应 System.out.println("Response Code : " + responseCode); System.out.println("Response Body : " + response.toString()); } } ``` 在代码中,我们首先建立一个URL对象,然后使用HttpURLConnection打开连接。我们设置HTTP请求方法为POST,并设置报文头的expect参数为"100-continue"。接下来,我们设置请求体格式为XML,并设置请求体并发送请求。最后,我们获取响应并输出响应码和响应体。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

情绪稳定的犟种

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值