红鱼儿

公告

统计

  • 随笔 - 137
  • 文章 - 1
  • 评论 - 29
  • 引用 - 0
用NetHttpClient执行Post操作遇到的问题

最近在做接口的调试,用NetHttpClient来进行相关操作。部分数据是用get方法来操作的,没有问题,但有个数据是用Post来操作的,始终报错,用了抓包分析工具发现没有发送数据出去。但找不出代码的问题,请大家帮忙看看。
关键部分代码如下:

procedure TTabbedwithNavigationForm.Button3Click(Sender: TObject);
var joj,jodata:tjsonobject;      
    jadata:tjsonarray;
    ss:tstringstream;        
    aresponse:ihttpresponse;
begin
 .........
  ss:=tstringstream.Create('',tencoding.UTF8);
  ss.WriteString(jodata.ToString);
  showmessage(inttostr(length(jodata.ToString)));

  nethttpclient1.Accept:='*/*';
  nethttpclient1.AcceptEncoding:='gzip,deflate';
  nethttpclient1.AcceptLanguage:='zh-CN';
  nethttpclient1.UserAgent:='Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko';
  nethttpclient1.ContentType:='application/json';
  nethttpclient1.CustomHeaders['Authorization']:=au_edit.Text;

  aresponse:=nethttpclient1.Post(lbltitle4.Text,ss);
  memo3.Lines.Add(aresponse.ContentAsString());

  ss.Free;
end;

运行代码后返回错误提示如下:
{"timestamp":"2019-06-14 16:53:37","status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: public com.qianfan123.sail.inf.service.ResponseResult<java.util.List<com.qianfan123.sail.inf.service.mbr.coupon.v2.CouponVT>> com.qianfan123.sail.inf.server.mbr.coupon.v2.CouponServiceVTImpl.issue(java.lang.String,java.lang.String,java.lang.String,com.qianfan123.sail.inf.service.mbr.coupon.v2.CouponIssueVTDTO)","path":"/api/mbr/v2/coupon/issue"}

通过抓包软件分析获取的正确执行的会话信息如下(*号是屏弊部分):
POST http://**********/api/mbr/v2/coupon/issue HTTP/1.1
Content-Type: application/json
Accept: */*
Authorization: **********
Referer: http://**********/swagger-ui.html
Accept-Language: zh-CN
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Length: 366
Host: ****.****.****:6020
Connection: Keep-Alive
Pragma: no-cache

{"event":"string","lines":[{"amount":"1000","beginDate":"2019-06-14 16:02:14","categoryCode":"000275","code":"20190614160214","count":"1","endDate":"2019-06-14 16:02:14"}],"memberId":"135057337785253888","occuredAt":"****","occuredTime":"2019-06-14 16:02:14","orgCode":"****","ownerActivity":"pos","summary":"测试","terminal":"01","transId":"20190614160214"}
HTTP/1.1 200
X-Application-Context: member-inf:10000
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 14 Jun 2019 08:04:27 GMT

1d7
{"status":"0000","message":"请求成功","data":[{"orgCode":"****","memberId":"******","code":"20190614160214","categoryId":"*****4","categoryCode":"******","name":"TIMTEST","type":"cash","facedAmt":1000,"amount":1000,"effectBegin":"2019-06-14 00:00:00","effectEnd":"2019-06-14 23:59:59","state":"可使用","fixed":true,"discount":null,"storeCode":null,"useLimitAmount":null,"isRegifted":-1,"limitRegiftedNum":null,"remainRegiftedNum":null}]}
0

而错误的抓包信息如下:(*号是屏弊部分)
POST http://*.*.*.*:6020/api/mbr/v2/coupon/issue HTTP/1.1
Content-Type: application/json
Accept: */*
Authorization:**********

Accept-Language: zh-CN
Accept-Encoding: gzip,deflate
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: *.*.*.*:6020
Content-Length: 0----------
Connection: Keep-Alive


HTTP/1.1 400
X-Application-Context: member-inf:10000
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 14 Jun 2019 08:21:33 GMT
Connection: close

21f
{"timestamp":"2019-06-14 16:21:33","status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing: public com.qianfan123.sail.inf.service.ResponseResult<java.util.List<com.qianfan123.sail.inf.service.mbr.coupon.v2.CouponVT>> com.qianfan123.sail.inf.server.mbr.coupon.v2.CouponServiceVTImpl.issue(java.lang.String,java.lang.String,java.lang.String,com.qianfan123.sail.inf.service.mbr.coupon.v2.CouponIssueVTDTO)","path":"/api/mbr/v2/coupon/issue"}
0

分析了下,抓包的差异主要在于错误部分缺少了:
Pragma: no-cache

{"event":"string","lines":[{"amount":"1000","beginDate":"2019-06-14 16:02..........
这段数据。也就是发送部分的数据感觉没有出去。

有个朋友也遇到了跟我一样的问题,结果不是什么body,head的问题,而是那个数据流写入后需要将其Position:=0,即可。如下:

  ss:=tstringstream.Create('',tencoding.UTF8);
  ss.WriteString(jodata.ToString);
  ss.Position:=0;

http://bbs.2ccc.com/topic.asp?topicid=566930,这是原文地址,感谢zhangpuqing分享!

后记:我在10.3.1下测试,当ss.WriteString(jodata.ToString)执行后,Position指向最后一个位置!如果我们在建立对象时,直接传String进去,则Position为0,例如:

 

  ss:=tstringstream.Create(jodata.ToString,tencoding.UTF8);
  //Position现在为0.

 

那这个问题,按上面的写法也可以,这样更简洁!

 

posted on 2019-06-28 08:22 红鱼儿 阅读(...) 评论(...) 编辑 收藏