首页 新闻 会员 周边

windowform 调用webAPI超时

0
悬赏园豆:20 [待解决问题]

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有问题?

嗷大喵学编程的主页 嗷大喵学编程 | 初学一级 | 园豆:165
提问于:2018-08-02 15:51
< >
分享
所有回答(2)
0

下面的代码有问题,没有结束请求流,服务器会一直等待发送请求内容

request.GetRequestStream().Write(buffer, 0, buffer.Length);

建议改为下面的代码

using(var sw = new StreamWriter(request.GetRequestStream()))
{
    sw.Write(sJson);
}
dudu | 园豆:31003 (高人七级) | 2018-08-02 16:39

按照你的办法尝试了,还是不行,我借助:postman,却可以有结果

支持(0) 反对(0) 嗷大喵学编程 | 园豆:165 (初学一级) | 2018-08-02 17:55

@嗷大喵学编程: 刚发现在读取响应流之前,response 被你关闭了 —— response.Close();

支持(0) 反对(0) dudu | 园豆:31003 (高人七级) | 2018-08-02 17:59

@dudu: 还没到读取响应流之前,就超时。request.GetResponse()

支持(0) 反对(0) 嗷大喵学编程 | 园豆:165 (初学一级) | 2018-08-02 18:13

@嗷大喵学编程: 确认一下是不是你电脑上的防火墙不允许

支持(0) 反对(0) dudu | 园豆:31003 (高人七级) | 2018-08-02 18:26
0

            request.ServicePoint.Expect100Continue = false;

放逐人 | 园豆:694 (小虾三级) | 2018-08-02 19:03

好像是下面的意思:如果你有100 continue,服务器会在响应100 continue后再来接收content内容,

而忽略之前请求中的content。

没有这个头就正常了。

支持(0) 反对(0) 放逐人 | 园豆:694 (小虾三级) | 2018-08-02 19:06
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册