System.GC.Collect();
string sJson = "{\"cardNo\":\"F22760587\",\"chargeId\":\"4200000122201808010163123885\",\"sOperator\":\"zzj\",\"ssBillId\":\"03957334\",\"sign\":\"78ACF3D0982B33724F0F5055FD273C45\"}";
string url = "http://xxxxx.ngrok.yzliusha.cn:8081/ssinsurance/expose/againstMzfee";
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Accept = "text/html, application/xhtml+xml, */*";
request.ContentType = "application/json";
byte[] buffer = encoding.GetBytes(sJson);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string a = reader.ReadToEnd();
}
当运行到:HttpWebResponse response = (HttpWebResponse)request.GetResponse();,会抛出异常,提示操作超时?
是我调用的有问题,还是对方提供的API有问题?
下面的代码有问题,没有结束请求流,服务器会一直等待发送请求内容
request.GetRequestStream().Write(buffer, 0, buffer.Length);
建议改为下面的代码
using(var sw = new StreamWriter(request.GetRequestStream()))
{
sw.Write(sJson);
}
按照你的办法尝试了,还是不行,我借助:postman,却可以有结果
@嗷大喵学编程: 刚发现在读取响应流之前,response 被你关闭了 —— response.Close();
@dudu: 还没到读取响应流之前,就超时。request.GetResponse()
@嗷大喵学编程: 确认一下是不是你电脑上的防火墙不允许
request.ServicePoint.Expect100Continue = false;
好像是下面的意思:如果你有100 continue,服务器会在响应100 continue后再来接收content内容,
而忽略之前请求中的content。
没有这个头就正常了。