邮政请求可以从邮递员那里获取,但不能从c#代码获取

时间:2019-07-19 07:45:50

标签: c# httpwebrequest postman restsharp

Post请求似乎在Postman上正常工作,但同样的事情在c#代码上却无效。但是,当我尝试从本地系统运行相同的代码时,它返回响应,但是当我从服务器尝试时,它不起作用并引发异常,底层连接已关闭:发生意外错误 发送。

我加入了安全协议来解决此问题,但没有帮助

C#代码:

var client = new RestClient("https://openweb.nlb.gov.sg/ows/CatalogueService.svc");

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;

var request = new RestRequest(Method.POST);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Connection", "keep-alive");
request.AddHeader("content-length", "714");
request.AddHeader("accept-encoding", "gzip, deflate");
request.AddHeader("cookie", "NSC_PQFOXFC-80=ffffffffaf1617ac45525d5f4f58455e445a4a423660; ASP.NET_SessionId=t12ibw24lljs5s2ktqrfrx45; NSC_PQFOXFC-443=ffffffffaf1617af45525d5f4f58455e445a4a42378b");
request.AddHeader("Host", "openweb.nlb.gov.sg");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("SOAPAction", "http://www.nlb.gov.sg/ws/CatalogueService/ICatalogueService/Search");
request.AddHeader("Content-Type", "text/xml;charset=utf-8");
request.AddParameter("undefined", "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body \r\nxmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n<SearchRequest xmlns=\"http://www.nlb.gov.sg/ws/CatalogueService\">\r\n<APIKey>UFJPRC1EU1RBMjAxODpEU1RBQE9wM25XM2JOTEIhQCM=</APIKey>\r\n<SearchItems>\r\n<SearchItem>\r\n<SearchField>keywords</SearchField>\r\n<SearchTerms>blue ocean strategy</SearchTerms>\r\n</SearchItem>\r\n</SearchItems>\r\n<ContentType>eBooks</ContentType>\r\n<DataFrom>Apabi,Ebrary,Gale,LMS,Netlibrary,NHB,NLB,Overdrive,Wel</DataFrom>\r\n<Modifiers>\r\n<StartRecordPosition>0</StartRecordPosition>\r\n<MaximumRecords>20</MaximumRecords>\r\n</Modifiers>\r\n</SearchRequest>\r\n</s:Body>\r\n</s:Envelope>", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

我希望显示结果,但会引发异常

System.Net.WebException: The underlying connection was closed: An unex
pected error occurred on a send. ---> System.IO.IOException: Authentication fail
ed because the remote party has closed the transport stream.
   at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes
, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocol
Request asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToke
n message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, A
syncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byt
e[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyRes
ult)
   at System.Net.TlsStream.CallProcessAuthentication(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionCo
ntext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.ConnectStream.WriteHeaders(Boolean async)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at RestSharp.Http.WriteRequestBody(HttpWebRequest webRequest)
   at RestSharp.Http.PostPutInternal(String method)
   at RestSharp.Http.AsPost(String httpMethod)
   at RestSharp.RestClient.DoExecuteAsPost(IHttp http, String method)
   at RestSharp.RestClient.Execute(IRestRequest request, String httpMethod, Func
`3 getResponse)

0 个答案:

没有答案