0

其实我有这样的事情:

private void createHttpRequest()
    {
        System.Uri myUri = new System.Uri("..url..");
        HttpWebRequest myHttpRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
        myHttpRequest.Method = "POST";
        myHttpRequest.ContentType = "application/x-www-form-urlencoded";
        myHttpRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myHttpRequest);
    }

void GetRequestStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
        // End the stream request operation
        Stream postStream = myRequest.EndGetRequestStream(callbackResult);

        string hash = HashHelper.createStringHash("123", "TEST", "0216");
        // Create the post data
        byte[] byteArray = createByteArrayFromHash(hash);

        // Add the post data to the web request
        postStream.Write(byteArray, 0, byteArray.Length);
        postStream.Close();

        // Start the web request
        myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
    }

 void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
        using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
        {
            string result = httpWebStreamReader.ReadToEnd();
            ApiResponse apiResponse = (ApiResponse)JsonConvert.DeserializeObject<ApiResponse>(result);
        }
    }

很好,它正在工作,但现在我必须在每个页面中使用这些方法,只需更改创建请求的方法 createByteArrayFromHash。如果我想创建可以帮助我在页面中大约 3 行代码中执行此操作的辅助类怎么办。你会怎么做?我正在考虑这种方式,但是如何在响应之前添加请求?或者你会用另一种方式吗?谢谢

4

1 回答 1

2

是的,最好使用asyncand await。以下是此类包装器的示例:

public async Task<string> SendRequestGetResponse(string postData, CookieContainer cookiesContainer = null)
{
    var postRequest = (HttpWebRequest)WebRequest.Create(Constants.WebServiceUrl);
    postRequest.ContentType = "Your content-type";
    postRequest.Method = "POST";
    postRequest.CookieContainer = new CookieContainer();
    postRequest.CookieContainer = App.Session.Cookies;

    using (var requestStream = await postRequest.GetRequestStreamAsync())
    {
        byte[] postDataArray = Encoding.UTF8.GetBytes(postData);
        await requestStream.WriteAsync(postDataArray, 0, postDataArray.Length);
    }

    var postResponse = await postRequest.GetResponseAsync() as HttpWebResponse;

    if (postResponse != null)
    {
        var postResponseStream = postResponse.GetResponseStream();
        var postStreamReader = new StreamReader(postResponseStream);

        // Can use cookies if you need
        if (cookiesContainer == null)
        {
            if (!string.IsNullOrEmpty(postResponse.Headers["YourCookieHere"]))
            {
                var cookiesCollection = postResponse.Cookies;
                // App.Session is a global object to store cookies and etc.
                App.Session.Cookies.Add(new Uri(Constants.WebServiceUrl), cookiesCollection);
            }
        }

        string response = await postStreamReader.ReadToEndAsync();
        return response;
    }
    return null;
}

你可以随意修改

于 2013-02-14T12:24:08.760 回答