【问题标题】:Http GET request from windows phone 8来自 Windows Phone 8 的 Http GET 请求
【发布时间】:2014-03-16 03:03:59
【问题描述】:

此代码适用于 Windows 窗体:

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);
}

但我想从 windows phone 8 发送 http GET 请求。在 wp 8 中没有方法 UploadString() 等...

【问题讨论】:

  • 你想发送http GET但使用UploadString?
  • UploadString 在 windows phone 8 中被删除方法

标签: c# windows-phone-8


【解决方案1】:

只需使用HttpClient

using(HttpClient hc = new HttpClient())
{
    var response = await hc.PostAsync(url,new StringContent (yourString));
}

对于您的情况,您可以上传FormUrlEncodedContent 内容,而不是手动形成上传字符串。

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);
}

【讨论】:

    【解决方案2】:

    试试HTTP Client NuGet 库。它适用于 Windows Phone 8。

    【讨论】:

      【解决方案3】:

      对于 GET 方法,您可以提供字符串以及 URI 本身。

      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;
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        已经在这里回答:Http Post for Windows Phone 8 -- 你会想要这样的:

        // 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);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-10-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多