0

我有一个带有自定义 WebPost 方法的 JavaScriptHelper 类,如下所示:

public string WebPost(string url, string json)
        {
            #region

           string responseData = "";
            try
           {
                HttpWebRequest webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;

                StreamWriter requestWriter = null;
                webRequest.Method = "POST";
                webRequest.ContentType = "application/json";

                using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
                {
                    requestWriter.Write(json);
                }

                using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
                {
                    responseData = responseReader.ReadToEnd();
                }

                webRequest = null;
            }
            catch (WebException ex)
            {
                LogBase.LogController.WriteError(ex);
            }
            catch (Exception ex)
            {
                LogBase.LogController.WriteError(ex);
            }
            return responseData;
            #endregion
        }

我使用它向 ASP.NET WebAPI 服务发送 POST 请求,如下所示:

RegisterModel model = new RegisterModel();
model.email = "abc@gmail.com";
model.oAuthId = "11111111";
model.oAuthType = "Facebook";

string jsonStr = JsonConvert.SerializeObject(model);
string response = new JavaScriptHelper().WebPost("http://localhost:9091/api/Authentication/authenticateoauth", jsonStr);

服务接收此调用。如果用户存在,则返回响应,如果不存在,则创建用户并返回响应。

我面临的问题是,当用户存在时,一切正常。我在客户端(JSON)上得到响应。当它不存在时,它会在服务器上正确创建用户,但我的客户端提前中断并且不等待响应。我收到以下错误:

System.Net.WebException was unhandled
  HResult=-2146233079
  Message=The remote server returned an error: (500) Internal Server Error.
  Source=System
  StackTrace:
       at System.Net.HttpWebRequest.GetResponse()

如何强制我的请求等待响应时间更长一点?

4

0 回答 0