i have a windows phone project and i am trying to post data to website and get the data
i have working windows form project code but when i try to use that code on wp8 i get error says

Error 2 'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?) C:\Users\cihad\documents\visual studio 2012\Projects\molitva\molitva\vaktija.xaml.cs 91 61 molitva

here is my code

public string GetHtml(int id)
        {
            var request = (HttpWebRequest)WebRequest.Create("http://www.vaktija.ba/mobile/");
            request.Method = "POST";
            byte[] byteArray = System.Text.Encoding.UTF8.GetBytes("gradovi=" + id);
            request.ContentLength = byteArray.Length;
            var dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            WebResponse response = (HttpWebResponse)request.GetResponse();
            dataStream = response.GetResponseStream();
            var reader = new System.IO.StreamReader(dataStream);
            var html = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();
            return html;
        }

so what can i do

Dani AI

Generated

The compiler error shown in ’s post is not just a missing using (as asked). On Windows Phone 8 the Silverlight networking API does not expose the synchronous GetRequestStream / GetResponse calls from desktop .NET. That is why the call cannot be found. Two practical approaches: use the async Begin/End pattern on HttpWebRequest, or use HttpClient with async/await (recommended — simpler and easier to read).

Example using HttpClient (keeps the UI responsive and sets the correct form content-type automatically):

using System.Net.Http;
using System.Threading.Tasks;

public async Task<string> PostGradoviAsync(int id)
{
    using (var client = new HttpClient())
    {
        var content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string,string>("gradovi", id.ToString())
        });
        var resp = await client.PostAsync("https://example.com/mobile", content);
        resp.EnsureSuccessStatusCode();
        return await resp.Content.ReadAsStringAsync();
    }
}

Important notes and troubleshooting

  • If HttpClient types are missing, add the Microsoft HTTP Client Libraries (Microsoft.Net.Http) via NuGet and include using System.Net.Http;.
  • Do not block the UI thread with .Result or .Wait(); call the async method from an async event handler and await it.
  • Confirm the app has internet/network capability enabled in WMAppManifest.xml (Internet / networking capability).
  • If sticking with HttpWebRequest, replace the sync calls with BeginGetRequestStream/EndGetRequestStream and BeginGetResponse/EndGetResponse.
  • Ensure the server expects application/x-www-form-urlencoded (FormUrlEncodedContent does this). If responses are unexpected, examine status codes and exception details to narrow the cause.

Can you confirm you have added the correct USING statement?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.