C#调用JavaHttp接口抛500内部错误

  最近对接一个项目,需要把数据传输到人家平台。对方是用Java写的接口,我们这边用的是C#。接口是POST类型,参数是Json类型。

  C#在调用的过程myRequest.GetResponse()这句一直抛500内部错误。然后写了个Java进行测试,一样地址和参数可以调用成功。这下郁闷了。C#和JAVA有什么不同呢。试了很多种方法。还是不能解决500内部错误问题。

  后来只能上大招用了Fiddler抓取HTTP请求。不抓不知道,一抓吓一跳。其实很简单,抓包的结果中多处了一下字符串“‍Expect:100-Continue”。

  查询“‍Expect:100-Continue”含义

  The purpose of the 100 (Continue) status (see section 10.1.1) is to allow a client that is sending a request message with a request body to determine if the origin server is willing to accept the request (based on the request headers) before the client sends the request body. In some cases, it might either be inappropriate or highly inefficient for the client to send the body if the server will reject the message without looking at the body.

(100状态码(继续,见10.1.1节)的目的在于允许客户端判定服务器是否愿意接受客户端发来的消息主体(基于请求头域)在客户端发送此请求消息主体前。 在有些情况下,如果服务器拒绝查看消息主体,这时客户端发送消息主体是不合适的或会降低效率。)

 

  就是说 ‍Expect:100-Continue的作用是,设定Client 和 Server在Post数据前需要进行 ‍“请求头域” 的数据匹配,相当于是握手。如果匹配则开始进行body 的内容,Post数据。

解决办法:

httpWebRequest.ServicePoint.Expect100Continue = false;

把Expect100Continue设置为false。不再验证。直接POST。

 

posted @ 2019-06-19 09:12  ﹎潴潴﹖  阅读(1335)  评论(1编辑  收藏  举报