3

I have two methods

public void GetEmp()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";

    request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);  

}

and

private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
    HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
    using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult))
    {
        using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
        {
            string results = httpwebStreamReader.ReadToEnd();
            //execute UI stuff on UI thread.              
        }
    }
}  

Here i want to return a string "results" to some other method like this

string data= obj1.GetEmp()

How can i achieve this.. Any help would be appriciated.. Thanks

1
  • Why dont you use GetResponse() if you want a synchronous [blocking ] call? Commented Apr 19, 2016 at 6:20

2 Answers 2

5

Easiest way to do this is rewrite method using async, like this:

public async Task<string> GetEmpAsync()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";
    var response = await request.GetResponseAsync();
    using (StreamReader httpwebStreamReader = new StreamReader(response.GetResponseStream()))
    {
        string results = await httpwebStreamReader.ReadToEndAsync();
        //execute UI stuff on UI thread.
        return results;
    }

}

Then you can get result with code like this:

var results = await GetEmpAsync();

If you are using older version and don't have async, you can do it in blocking way to get result:

public string GetEmpBlocking()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";
    var response = request.GetResponse();
    using (StreamReader httpwebStreamReader = new StreamReader(response.GetResponseStream()))
    {
        string results = httpwebStreamReader.ReadToEnd();
        //execute UI stuff on UI thread.
        return results;
    }
}

And get result like this:

var results = GetResultBlocking();

Р.S. you can also consider using Microsoft.Bcl.Async to support async in earlier versions.

Sign up to request clarification or add additional context in comments.

5 Comments

at this line var results = await GetempAsync(); i am getting this error "MyClass' does not contain a definition for 'GetempAsync' and no extension method 'GetempAsync' accepting a first argument of type 'MyClass' could be found (are you missing a using directive or an assembly reference?)"
@SagarJagadesh sorry there was a typo, there should be var results = await GetEmpAsync();. Basicaly it's just call of your method with await keyword before it.
Hi i am getting error as "Unknown identifier: request " at this line "await request.GetResponseAsync(); " can u please suggest ?
@SagarJagadesh what version of .Net are you using? If it's 4.5 or upper please check that you added 'async' to method definition. If it's less than 4.5 you need add Microsoft.Bcl.Async lib to support async.
i just replaced this line " var response = await request.GetResponseAsync();" with new line "var response = (HttpWebResponse) await request.GetResponseAsync().ConfigureAwait(false);".. Its working fine..Thanks for the help..
2

Just use the

var resp = request.GetResponse();

instead of the

request.BeginGetResponse(...);

With the synchrounous GetResonse, you can return the string directly.

public string GetEmp()
{
    var request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri(@"http://.../data.json"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";

    var resp = request.GetResponse();

    using (var httpWebStreamReader = new StreamReader(resp.GetResponseStream()))
    {
        var result = httpWebStreamReader.ReadToEnd();
        return result;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.