コード例 #1
0
    // tools for making basic requests - we must be logged in to use them
    // Post uses the http POST method and expects extra data to send
    private string PostRequest(string uri, string dataString)
    {
        string result = null;

        if (webClientValid == WebClientState.FAILED)
        {
            return(null);
        }
        using (webClient = GetNewWebClient())
        {
            try
            {
                // if this auth cookie is invalid nothing will work ...
                webClient.Headers.Add("Content-Type", "text/plain");
                webClient.Headers.Add("Cookie", auth);
                result = webClient.UploadString(uri, "POST", dataString);
            }
            catch (Exception e)
            {
                Debug.Log("GET failed: " + e.Message);
                webClientValid = WebClientState.FAILED;
            }
        }
        webClient.Dispose();
        return(result);
    }
コード例 #2
0
    // Get uses http's GET method and only needs a url
    private string GetRequest(string uri)
    {
        string result = null;

        if (webClientValid == WebClientState.FAILED)
        {
            return(null);
        }
        using (webClient = GetNewWebClient())
        {
            try
            {
                webClient.Headers.Add("Content-Type", "text/plain");
                webClient.Headers.Add("Cookie", auth);
                result         = webClient.DownloadString(uri);
                webClientValid = WebClientState.CONNECTED;
            }
            catch (Exception e)
            {
                Debug.Log("POST failed: " + e.Message);
                webClientValid = WebClientState.FAILED;
            }
        }
        webClient.Dispose();
        return(result);
    }
コード例 #3
0
 /// <summary>
 /// 异步获取数据
 /// </summary>
 public void AsyncGet()
 {
     this._outType    = 0;
     this._state      = WebClientState.Sending;
     this._saveStream = new MemoryStream();
     //_req.BeginGetResponse(OnAsyncGetResponse, this);
     ThreadPool.QueueUserWorkItem(this.StartGetResponse);
 }
コード例 #4
0
 /// <summary>
 /// 异步请求资源并保存到流
 /// </summary>
 /// <param name="saveStream">输出流</param>
 public void AsyncToStream(Stream saveStream)
 {
     this._outType    = 1;
     this._state      = WebClientState.Sending;
     this._saveStream = saveStream;
     //_req.BeginGetResponse(OnAsyncGetResponse, this);
     ThreadPool.QueueUserWorkItem(this.StartGetResponse);
 }
コード例 #5
0
 /// <summary>
 /// 异步请求资源并保存到文件
 /// </summary>
 /// <param name="saveFile">输出文件名</param>
 public void AsyncToFile(string saveFile)
 {
     this._saveFile   = saveFile;
     this._outType    = 2;
     this._state      = WebClientState.Sending;
     this._saveStream = File.Open(saveFile, FileMode.Create, FileAccess.Write, FileShare.None);
     //_req.BeginGetResponse(OnAsyncGetResponse, this);
     ThreadPool.QueueUserWorkItem(this.StartGetResponse);
 }
コード例 #6
0
 /// <summary>
 /// 异步请求提交数据并保存到流
 /// </summary>
 /// <param name="dataStream">输入数据流</param>
 /// <param name="saveStream">输出流</param>
 public void AsyncPostAndToStream(Stream dataStream, Stream saveStream)
 {
     this._outType    = 1;
     this._state      = WebClientState.Sending;
     this._saveStream = saveStream;
     this._req.Method = "POST";
     //_req.BeginGetRequestStream(OnAsyncGetRequestStream, dataStream);
     ThreadPool.QueueUserWorkItem(this.StartGetRequestStream, dataStream);
 }
コード例 #7
0
 /// <summary>
 /// 异步请求提交数据并获取数据
 /// </summary>
 /// <param name="postData">提交数据</param>
 public void AsyncPost(byte[] postData)
 {
     this._outType    = 0;
     this._state      = WebClientState.Sending;
     this._saveStream = new MemoryStream();
     this._req.Method = "POST";
     //_req.BeginGetRequestStream(OnAsyncGetRequestStream, postData);
     ThreadPool.QueueUserWorkItem(this.StartGetRequestStream, postData);
 }
コード例 #8
0
 /// <summary>
 /// 异步请求提交数据并保存到文件
 /// </summary>
 /// <param name="dataStream">输入数据流</param>
 /// <param name="saveFile">输出文件名</param>
 public void AsyncPostAndToFile(Stream dataStream, string saveFile)
 {
     this._saveFile   = saveFile;
     this._outType    = 2;
     this._state      = WebClientState.Sending;
     this._saveStream = File.Open(saveFile, FileMode.Create, FileAccess.Write, FileShare.None);
     this._req.Method = "POST";
     //_req.BeginGetRequestStream(OnAsyncGetRequestStream, dataStream);
     ThreadPool.QueueUserWorkItem(this.StartGetRequestStream, dataStream);
 }
コード例 #9
0
        /// <summary>
        /// 请求获得WebResponse对象
        /// </summary>
        /// <returns></returns>
        public WebResponse GetResponse()
        {
            this._size    = 0;
            this._recived = 0;
            this._state   = WebClientState.Sending;
            this._resp    = this._req.GetResponse();

            this._size  = this._resp.ContentLength;
            this._state = WebClientState.Reciving;
            return(this._resp);
        }
コード例 #10
0
        /// <summary>
        /// 关闭对象,释放资源
        /// </summary>
        public virtual void Dispose()
        {
            if (this._isDisposed)
            {
                return;
            }

            if (this._req != null)
            {
                try
                {
                    this._req.Abort();
                }
                catch
                { }
            }

            this._stream?.Close();

            if (this._resp != null)
            {
                try
                {
                    this._resp.Close();
                }
                catch
                { }
            }

            if (this._outType == 2)
            {
                this._saveStream.Close();
                if (!string.IsNullOrEmpty(this._error))                     // error
                {
                    try
                    {
                        File.Delete(this._saveFile);
                    }
                    catch
                    { }
                }
            }

            this._state = WebClientState.Finished;
            this._error = "Closed";

            this._req        = null;
            this._resp       = null;
            this._stream     = null;
            this._saveStream = null;
            this._buf        = null;
            this._isDisposed = true;
        }
コード例 #11
0
 protected virtual void OnAsyncGetResponse()
 {
     try
     {
         this._stream = this._resp.GetResponseStream();
         this._stream.BeginRead(this._buf, 0, this._buf.Length, this.OnAsyncReciveData, this);
     }
     catch (Exception e)
     {
         this._error = "Get response input stream error! [" + e.Message + "]";
         this._state = WebClientState.Error;
     }
 }
コード例 #12
0
        /// <summary>
        /// 同步保存数据到流
        /// </summary>
        public virtual void ToStream(Stream saveStream)
        {
            this._outType    = 1;
            this._saveStream = saveStream;

            if (this._resp == null)
            {
                this.GetResponse();
            }

            this._stream = this._resp.GetResponseStream();
            this.CopyStream(this._stream, this._saveStream);
            this._state = WebClientState.Finished;
        }
コード例 #13
0
        /// <summary>
        /// 同步请求Post提交数据
        /// </summary>
        /// <param name="postData">提交数据</param>
        public virtual void PostData(byte[] postData)
        {
            this._size    = postData.Length;
            this._recived = 0;
            this._state   = WebClientState.Sending;
            Stream os = this._req.GetRequestStream();

            while (this._recived < this._size)
            {
                int bs = ( int )MathUtils.Min(this._buf.Length, this._size - this._recived);
                os.Write(postData, ( int )this._recived, bs);
                this._recived += bs;
            }
        }
コード例 #14
0
        /// <summary>
        /// 同步保存数据到文件
        /// </summary>
        public virtual void ToFile(string saveFile)
        {
            this._saveFile   = saveFile;
            this._outType    = 2;
            this._saveStream = File.Open(saveFile, FileMode.Create, FileAccess.Write, FileShare.None);

            if (this._resp == null)
            {
                this.GetResponse();
            }

            this._stream = this._resp.GetResponseStream();
            this.CopyStream(this._stream, this._saveStream);
            this._saveStream.Close();
            this._state = WebClientState.Finished;
        }
コード例 #15
0
        /// <summary>
        /// 同步获取数据
        /// </summary>
        public virtual byte[] GetBytes()
        {
            this._outType    = 0;
            this._saveStream = new MemoryStream();

            if (this._resp == null)
            {
                this.GetResponse();
            }

            this._stream = this._resp.GetResponseStream();
            this.CopyStream(this._stream, this._saveStream);
            this._saveStream.Close();
            this._state = WebClientState.Finished;
            return(this.bytes);
        }
コード例 #16
0
        private void OnAsyncReciveData(IAsyncResult result)
        {
            if (this.isDone)
            {
                return;
            }

            try
            {
                int len = this._stream.EndRead(result);
                if (len > 0)
                {
                    this._recived += len;

                    try
                    {
                        this._saveStream.Write(this._buf, 0, len);
                    }
                    catch (Exception e)
                    {
                        this._error = "Write data error! [" + e.Message + "]";
                        this._state = WebClientState.Error;
                        return;
                    }

                    this._stream.BeginRead(this._buf, 0, this._buf.Length, this.OnAsyncReciveData, this);
                }
                else
                {
                    this._stream.Close();
                    this._saveStream.Flush();
                    if (this._outType != 1)
                    {
                        this._saveStream.Close();
                    }
                    this._state = WebClientState.Finished;
                }
            }
            catch (Exception e)
            {
                this._error = "Recive data error! [" + e.Message + "]";
                this._state = WebClientState.Error;
            }
        }
コード例 #17
0
 // logs in and as a side effect gets our auth cookie - needed for all other requests
 private bool Login()
 {
     loggedin = false;
     if (REMOTE_URI == null || REMOTE_SECRET == null)
     {
         Debug.Log("DataFarmer missing configuation needed to log in!");
         return(false);
     }
     using (webClient = GetNewWebClient())
     {
         string content = null;
         try
         {
             content        = webClient.DownloadString(makeNonce().Uri(string.Format("{0}/login", REMOTE_URI)));
             webClientValid = WebClientState.CONNECTED;
         }
         catch (Exception e)
         {
             Debug.Log("Login failed: " + e);
             webClientValid = WebClientState.FAILED;
             return(false);
         }
         Debug.Log("login result: " + content);
         if (!content.Contains("ERROR:"))
         {
             loggedin = true;
         }
         WebHeaderCollection headers = webClient.ResponseHeaders;
         int i = 0;
         for (; i < headers.Count; i++)
         {
             Debug.Log(headers.GetKey(i) + ": " + headers.Get(i));
             if (headers.GetKey(i) == "Set-Cookie")
             {
                 auth = headers.Get(i);
                 Debug.Log(string.Format("{0}", auth));
                 break;
             }
         }
         webClient.Dispose();
     }
     return(loggedin);
 }
コード例 #18
0
        private void OnAsyncGetResponse(IAsyncResult result)
        {
            if (this.isDone)
            {
                return;
            }

            try
            {
                this._resp    = this._req.EndGetResponse(result);
                this._size    = this._resp.ContentLength;
                this._recived = 0;
                this._state   = WebClientState.Reciving;
            }
            catch (Exception e)
            {
                this._error = "Get response error! [" + e.Message + "]";
                this._state = WebClientState.Error;
                return;
            }

            this.OnAsyncGetResponse();
        }
コード例 #19
0
        private void OnAsyncGetRequestStream(IAsyncResult result)
        {
            if (this.isDone)
            {
                return;
            }

            try
            {
                Stream os = this._req.EndGetRequestStream(result);

                object mState = result.AsyncState;
                if (mState is Stream)
                {
                    Stream stream = ( Stream )mState;
                    this._size = -1;
                    try
                    {
                        this._size = stream.Length;
                    }
                    catch
                    {
                        this._size = -1;
                    }
                    this._recived = 0;
                    do
                    {
                        int tmp = stream.Read(this._buf, 0, this._buf.Length);
                        if (tmp > 0)
                        {
                            os.Write(this._buf, 0, tmp);
                            this._recived += tmp;
                        }
                        else
                        {
                            break;
                        }
                    }while (true);
                }
                else
                {
                    byte[] data = ( byte[] )mState;
                    this._size    = data.Length;
                    this._recived = 0;
                    while (this._recived < this._size)
                    {
                        int bs = ( int )MathUtils.Min(this._buf.Length, this._size - this._recived);
                        os.Write(data, ( int )this._recived, bs);
                        this._recived += bs;
                    }
                }
                os.Close();
            }
            catch (Exception e)
            {
                this._error = "Send request data error! [" + e.Message + "]";
                this._state = WebClientState.Error;
                return;
            }

            try
            {
                this._req.BeginGetResponse(this.OnAsyncGetResponse, this);
            }
            catch (Exception e)
            {
                this._error = "Get response error! [" + e.Message + "]";
                this._state = WebClientState.Error;
            }
        }