httpwebrequest time for the operation expired



  • I use C++CLI in MVS 2015 Community (NET 4)

    My program works in the flow. The shape flow is the result and the designs are calculated. There's a static class that works from httpwebrequest. Exactly (part of class)

    Announcement:

    ref class http abstract sealed
    {
    public:
        static String^ httpGet(String^, http_params^);
        static String^ httpPost(String^, String^, http_params^);
    };
    

    Part of the definition

    String^ http::httpGet(String^ url, http_params ^params)
    {
        HttpWebRequest ^HReq = dynamic_cast<HttpWebRequest^>(WebRequest::Create(url));
        HReq->Method = L"GET";
        HReq->ProtocolVersion = HttpVersion::Version11;
        HReq->KeepAlive = false;
        HReq->Referer = params->referer;
        HReq->Timeout = 10000;
        HReq->UserAgent = L"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
        HReq->CookieContainer = params->cookie;
        HReq->Accept = L"*/*"; 
        HReq->Headers->Add(L"Accept-Language", L"ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
        HReq->Headers->Add(L"Accept-Encoding", L"gzip,deflate,sdch");
        HReq->AutomaticDecompression = DecompressionMethods::GZip;
        HReq->AllowAutoRedirect = params->is_loc;
        if (params->is_loc)
            HReq->MaximumAutomaticRedirections = 10;
    
    HttpWebResponse ^HRes = dynamic_cast&lt;HttpWebResponse^&gt;(HReq-&gt;GetResponse());
    
    Stream ^res_stream = HRes-&gt;GetResponseStream();
    
    StreamReader ^ StR = gcnew StreamReader(res_stream);
    String ^res = StR-&gt;ReadToEnd();
    StR-&gt;Close();
    
    res_stream-&gt;Close();
    
    HRes-&gt;Close();
    
    return res;
    

    }

    String^ http::httpPost(String^ url, String^ data, http_params ^params)
    {
    HttpWebRequest ^HReq = dynamic_cast<HttpWebRequest^>(WebRequest::Create(url));
    HReq->Method = L"POST";
    HReq->ServicePoint->ConnectionLeaseTimeout = 0;
    HReq->ProtocolVersion = HttpVersion::Version11;
    HReq->ServicePoint->Expect100Continue = false;
    HReq->KeepAlive = false;
    HReq->Referer = params->referer;
    HReq->Timeout = 10000;
    HReq->Accept = L"/";
    HReq->Headers->Add(L"Accept-Language",L"ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
    HReq->Headers->Add(L"Accept-Encoding", L"gzip,deflate,sdch");
    HReq->AutomaticDecompression = DecompressionMethods::GZip;
    HReq->UserAgent = L"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36";
    HReq->CookieContainer = params->cookie;
    HReq->AllowAutoRedirect = params->is_loc;
    if (params->is_loc)
    HReq->MaximumAutomaticRedirections = 10;
    HReq->ContentType = "application/x-www-form-urlencoded";
    array <Byte> ^dd = Encoding::UTF8->GetBytes(data);
    HReq->ContentLength = dd->Length;

    Stream ^srtwr = HReq-&gt;GetRequestStream();
    srtwr-&gt;Write(dd, 0, dd-&gt;Length);
    srtwr-&gt;Close();
    
    
    String ^res;
    HttpWebResponse ^HRes = dynamic_cast&lt;HttpWebResponse^&gt;(HReq-&gt;GetResponse());
    
    srtwr = HRes-&gt;GetResponseStream();
    
    StreamReader ^StR = gcnew StreamReader(srtwr, Encoding::UTF8);
    
    res = StR-&gt;ReadToEnd();
    
    
    StR-&gt;Close();
    
    srtwr-&gt;Close();
    
    HRes-&gt;Close();
    
    return res;
    

    }

    The problem is, it's a early problem.

    Stream ^srtwr = HReq->GetRequestStream();

    or

    srtwr = HRes->GetResponseStream();

    Never mind, get or post

    Exemption

    The exception of the type "System.Net.WebException" arose in System.dll, but not
    Additional information was processed in the user code: Time
    Operation expectations expired

    Sometimes it works normally, sometimes this exception happens all the time.



  • Exemption and second attempt. Networks, they do, sometimes they don't work.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2