I want to do this thing in windows phone 8.1 please suggest how to do.I have tried httpclient but didn't achieve the same result please suggest me something
private async void Button_Click(object sender, RoutedEventArgs e)
{
WebClient web = new WebClient();
web.Headers["content-type"] = "application/x-www-form-urlencoded";
string arg = "id=" + newone.Text;
// var postdata =js
string arg1 = "id=" + newone.Text;
//web.UploadStringAsync(new Uri("http://terasol.in/hoo/test.php/?id=&ncuavfvlqfd"), "GET");
web.UploadStringAsync(new Uri("http://terasol.in/hoo/test.php"), "POST", arg1);
web.UploadStringCompleted += web_uploadstringcomplete;
}
void web_uploadstringcomplete(object sender, UploadStringCompletedEventArgs e)
{
MessageBox.Show(e.Result);
}
thank you
Based on your sample code, and running it,
using the following code, I get the same value returned.
private static void Button_Click2(string id)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://terasol.in/");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("id", id)
});
var result = client.PostAsync("/hoo/test.php", content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(resultContent);
}
}
Related
I've been trying to post a message along with a link, I can send one POST request, but I am not sure how would one send two.
Here's my code:
private void Button2_Click(object sender, EventArgs e)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://graph.facebook.com");
string message = "hello";
string link = "www.facebook.com"
var payload = GetPayload(new {message});
HttpResponseMessage response2 = client.PostAsync($"me/feed?access_token={TextBox1.Text}", payload).Result;
}
}
private static StringContent GetPayload(object data)
{
var json = JsonConvert.SerializeObject(data);
return new StringContent(json, Encoding.UTF8, "application/json");
}
I am not sure how can I include the link too along with the message.
Al-right it turns out that the variable link should be passed as such:
var data = {message, link}
Thanks to chetan.
I have this Web Api call in my code behind. This is for my Single-Sign-On using Windows credential from our AD. What it did is just call the Web Api and check if the User is Authenticated and have access to the App.
I successfully call my Web Api using HttpClient.PostAsync but I'm wondering why it called several times in my Page_Load?
Please see below my Page_Load how I call the Web Api:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var languages = Language.Enabled().OrderByDescending(x => x.IsDefault);
var selected = languages.Where(x => x.Code == HSESA.Library.Helpers.Context.PublicLanguage().Code).Select(x => x.Code).FirstOrDefault();
Test();
}
}
And here is the Test() method and how I initialized HttpClient:
private static HttpClient client = new HttpClient();
protected void Test()
{
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string FromUrl = HttpContext.Current.Request.Url.AbsoluteUri;
#region Old Code
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("Login", user));
postData.Add(new KeyValuePair<string, string>("FromUrl", FromUrl));
System.Net.Http.HttpContent content = new System.Net.Http.FormUrlEncodedContent(postData);
string url = "http://localhost:1899";
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var response = client.PostAsync("/api/login/checkLogin", content).Result;
if (response.IsSuccessStatusCode)
{
LoginItemViewModel data = null;
string responseString = response.Content.ReadAsStringAsync().Result;
Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(responseString);
data = Newtonsoft.Json.JsonConvert.DeserializeObject<LoginItemViewModel>(responseString);
}
#endregion
}
Thanks in advance to someone that can help me with my problem.
I'm working on making a POST request using my Xamarin form to send data to an Action in my controller in my WebAPI project. The code with breakpoints doesn't go beyond
client.BaseAddress = new Uri("192.168.79.119:10000");
I have namespace System.Net.Http and using System mentioned in the code.
private void BtnSubmitClicked(object sender, EventArgs eventArgs)
{
System.Threading.Tasks.Task<HttpResponseMessage> statCode = ResetPassword();
App.Log(string.Format("Status Code", statCode));
}
public async Task<HttpResponseMessage> ResetPassword()
{
ForgotPassword model = new ForgotPassword();
model.Email = Email.Text;
var client = new HttpClient();
client.BaseAddress = new Uri("192.168.79.119:10000");
var content = new StringContent(
JsonConvert.SerializeObject(new { Email = Email.Text }));
HttpResponseMessage response = await client.PostAsync("/api/api/Account/PasswordReset", content); //the Address is correct
return response;
}
Need a way to make a Post request to that Action and sending that String or the Model.Email as a parameter.
You need to use a proper Uri and also await the Task being returned from the called method.
private async void BtnSubmitClicked(object sender, EventArgs eventArgs) {
HttpResponseMessage response = await ResetPasswordAsync();
App.Log(string.Format("Status Code: {0}", response.StatusCode));
}
public Task<HttpResponseMessage> ResetPasswordAsync() {
var model = new ForgotPassword() {
Email = Email.Text
};
var client = new HttpClient();
client.BaseAddress = new Uri("http://192.168.79.119:10000");
var json = JsonConvert.SerializeObject(model);
var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json");
var path = "api/api/Account/PasswordReset";
return client.PostAsync(path, content); //the Address is correct
}
I am trying to retrieve data from an authorization-restricted database which only contains text to my Windows Phone 8 app. My code looks like this:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
wc.Credentials = new NetworkCredential("username", "password");
wc.DownloadStringAsync(new Uri(#"http://www.siteaddress.com"));
}
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string text = e.Result;
}
Unfortunately I'm getting the following message. Am I doing anything wrong? Thanks in advance.
http://s30.postimg.org/yuc0mcb5t/pic.png
If you are trying to send data to the server to receive response from the server, then you mean POST data to server .
void GetPosts(string UserID)
{
WebClient webclient = new WebClient();
Uri uristring = new Uri("somelink.com");
webclient.Headers["Content-Type"] = "application/x-www-form-urlencoded";
string postJsonData = string.Empty;
postJsonData += "userId=" + UserID;//parameters you want to POST
webclient.UploadStringAsync(uristring, "POST", postJsonData);
webclient.UploadStringCompleted += webclient_UploadStringCompleted;
}
void webclient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
.... // any code
}
I'm developping on Windows phone 8 and I would like to know if it's possible to manipulate data in the same method when we call DownloadStringCompleted of WebClient?
private void DownloadDataFromWebService(String uri)
{
WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri(uri));
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
}
private void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
RootObject r = JsonConvert.DeserializeObject<RootObject>(e.Result);
List<Category> listeCategories = r.Result;
}
Thus, I would like to manage all code in only one method because I would like to return an object
For example,
private List<Category> GetCategories(String uri)
{
WebClient wc = new WebClient();
wc.DownloadStringAsync(new Uri(uri));
.....
.....
RootObject r = JsonConvert.DeserializeObject<RootObject>(e.Result);
return (List<Category>) r.Result;
}
Yes, that is possible, due to magic TaskCompletionSource class, http://msdn.microsoft.com/en-us/library/dd449174(v=vs.95).aspx . To download:
async Task<List<object>> getCategories(String uri)
{
var taskCompletionObj = new TaskCompletionSource<string>();
var wc= new webClient();
wc.DownloadStringAsync(new URI(uri, Urikind.Absolute)) += (o, e) =>
{
taskCompletionObj.TrySetResult(e.Result);
};
string rawString = await taskCompletionObj.Task;
RootObject r = JsonConvert.DeserializeObject<RootObject>(rawString);
return (List<Category>)r.Result;
}
To use: var x = await getCategories(myURI);