質問

I want to send data to a php page, which inserts it in a database. I got the following code from Sending data to php from windows phone from but it shows some errors:

  1. On using: System.Net.WebClient: type used in a using statement must be implicitly convertible to System.IDisposable.
  2. On UploadString: System.Net.WebClient does not contain a definition for UploadString and no extension method UploadString accepting a first argument of the type System.Net.WebClient could be found (are you missing a using directive or an assembly reference?).

Does anyone have an idea how to fix this?

string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1&param2=value2&param3=value3";

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, myParameters);
}
役に立ちましたか?

解決

http://www.drdobbs.com/windows/writing-your-first-windows-8-app-the-lay/240143752 says HttpClient is replacing WebClient in windows 8 app

Uploadstring uses post method to send data and PostAsync is available in HttpClient which is what you might need.

try something like this.

using System.Net.Http;
//Windows.Web.Http 


string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1&param2=value2&param3=value3"; 

sendData(URI,myParameters);


public async void sendData(string URI,string myParameters)
 {
    using(HttpClient hc = new HttpClient())
    {
        Var response = await hc.PostAsync(URI,new StringContent (myParameters));
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top