GetRequestStream() 超时异常

GetRequestStream() timeout exception

我想使用 Webclient 发出一个简单的 Http 请求:

public string PostRequest(object json, string contentType, string server)
{
    try
    {
        var request = (HttpWebRequest)WebRequest.Create(server);
        request.ContentType = contentType;
        request.Method = "POST";
        request.Timeout = 10000;
        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            streamWriter.Write(JsonConvert.SerializeObject(json));
        }


        var response = (HttpWebResponse)request.GetResponse();
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            return streamReader.ReadToEnd();
        }
    }
    catch (Exception e)
    {
        throw e;
    }

}

问题是 request.GetRequestStream() 部分永远不会 return 并且总是会超时(默认为 100 秒和 10 秒)。我正在使用带有 android 7 和更高版本 android 8.1 的 samsung xcover 4。服务器字符串在 PC.On 设备浏览器本身上复制到我的标准浏览器时工作正常,但它不起作用(超时)。内容类型为 "application/json".

我可以做些什么来解决这个问题,还是有其他方法可以在 xamarin 中发送未损坏的 httprequests?

服务器本身正在运行,我可以从我的设备 ping 它:

public int PingHost(string nameOrAddress)
{
    int pingCount = 0;
    Ping pinger = null;
    for (int i = 0; i < 4; i++)
    {
        try
        {
            pinger = new Ping();
            PingReply reply = pinger.Send(nameOrAddress);
            pingCount += reply.Status == IPStatus.Success ? 1:0;
        }
        catch (Exception){ pingCount = - 1; }
        finally
        {
            pinger?.Dispose();
        }
        if (pingCount == -1) return -1;

    }
    return pingCount;
}

感谢您抽出时间。

好吧,我的应用程序中有一个工作代码,它是这样的:

 public HttpClient apiClient;

GetType API

 public async Task<string> GetServiceData(string srvUrl)
    {
        try
        {
          apiClient = new HttpClient(new NativeMessageHandler(throwOnCaptiveNetwork: false, customSSLVerification: false)); // SSL true if you have custom SLL in your API 
          apiClient.BaseAddress = new Uri(_yourbaseUrl); // Url where the service is hosted
          apiClient.DefaultRequestHeaders.Add("",""); //defualt req header key value in case any
          var respon = await apiClient.GetAsync(srvUrl).Result.Content.ReadAsStringAsync(); // svrUrl is the name of the api that you want to consume 
          if (respon != string.Empty)
          {
               return respon;
          }
        }
        catch (HttpRequestException reqEx)
        {
           return string.Empty;
        }
        catch (Exception ex)
        {
           return string.Empty;
        }
    }

邮政类型API

  public async Task<string> PostServiceData(string srvUrl, object srvModel)
    {
        try
        {
          var myContent = JsonConvert.SerializeObject(srvModel);//your parameter to the API
          var stringContent = new StringContent(myContent, Encoding.UTF8, "application/json");
          apiClient = new HttpClient(new NativeMessageHandler(throwOnCaptiveNetwork: false, customSSLVerification: true));// SSL true if you have custom SLL in your API
          apiClient.BaseAddress = new Uri(_yourbaseUrl); // Url where the service is hosted
          apiClient.DefaultRequestHeaders.Add("",""); //defualt req header key value in case any
          apiClient.DefaultRequestHeaders.Accept
               .Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
            var respon = await apiClient.PostAsync(srvUrl, stringContent);
                var resReslt = respon.Content.ReadAsStringAsync().Result;
                return resReslt;
            }
            else
                return string.Empty;

        }
        catch (HttpRequestException reqEx)
        {
            return string.Empty;
        }
        catch (Exception ex)
        {
            return string.Empty;
        }

祝你好运万一查询回复!