Reputation: 427
This code works on windows forms :
string URI = "http://localhost/1/index.php?dsa=232323";
string myParameters = "";
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string HtmlResult = wc.UploadString(URI, myParameters);
}
But i want to send http GET request from windows phone 8 . in wp 8 there are no methods UploadString()
and etc...
Upvotes: 3
Views: 6187
Reputation: 116118
Simply use HttpClient
using(HttpClient hc = new HttpClient())
{
var response = await hc.PostAsync(url,new StringContent (yourString));
}
And for your case, you can upload FormUrlEncodedContent
content instead of forming upload string manually.
using(HttpClient hc = new HttpClient())
{
var keyValuePairs = new Dictionary<string,string>();
// Fill keyValuePairs
var content = new FormUrlEncodedContent(keyValuePairs);
var response = await hc.PostAsync(url, content);
}
Upvotes: 6
Reputation: 418
For GET method you can give the strings along with the URI itself.
private void YourCurrentMethod()
{
string URI = "http://localhost/1/index.php?dsa=232323";
string myParameters = "";
URI = URI + "&" + myParameters;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URI);
request.ContentType="application/x-www-form-urlencoded";
request.BeginGetResponse(GetResponseCallback, request);
}
void GetResponseCallback(IAsyncResult result)
{
HttpWebRequest request = result.AsyncState as HttpWebRequest;
if (request != null)
{
try
{
WebResponse response = request.EndGetResponse(result);
//Do what you want with this response
}
catch (WebException e)
{
return;
}
}
}
Upvotes: 0
Reputation: 9841
Try HTTP Client NuGet library. It works for me in Windows Phone 8.
Upvotes: 0
Reputation: 5190
Already answered here: Http Post for Windows Phone 8 -- you will want something like this:
// server to POST to
string url = "myserver.com/path/to/my/post";
// HTTP web request
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "text/plain; charset=utf-8";
httpWebRequest.Method = "POST";
// Write the request Asynchronously
using (var stream = await Task.Factory.FromAsync<Stream>(httpWebRequest.BeginGetRequestStream,
httpWebRequest.EndGetRequestStream, null))
{
//create some json string
string json = "{ \"my\" : \"json\" }";
// convert json to byte array
byte[] jsonAsBytes = Encoding.UTF8.GetBytes(json);
// Write the bytes to the stream
await stream.WriteAsync(jsonAsBytes, 0, jsonAsBytes.Length);
}
Upvotes: -2