如何在窗口电话中发送字符串到url

本文关键字:字符串 url 窗口 电话 | 更新日期: 2023-09-27 17:50:49

我在静态类(window phone)中创建了一个方法:

public  void testSend()
      {
          try
          {
              string url = "";
              string str = "test";
              HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
              req.Method = "POST";
              string Data = "data=" + str;
              byte[] postBytes = Encoding.UTF8.GetBytes(Data);
              req.ContentType = "application/x-www-form-urlencoded";
              req.ContentLength = postBytes.Length;
              Stream requestStream = req.GetRequestStream();
              requestStream.Write(postBytes, 0, postBytes.Length);
              requestStream.Close();
              HttpWebResponse response = (HttpWebResponse)req.GetResponse();
              Stream resStream = response.GetResponseStream();
              var sr = new StreamReader(response.GetResponseStream());
              string responseText = sr.ReadToEnd();

          }
          catch (WebException)
          {
          }

我不确定,但它得到错误的GetRequestStream()和GetResponse,因为它不包含在HttpWebRequest。

如何在窗口电话中发送字符串到url

您需要使用WebClient或HttpWebRequest。

web客户端示例=>

    public void testSend()
  {
              string url = "";
              string str = "test";
              HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
              req.Method = "POST";
              string Data = "data=" + str;
              byte[] postBytes = Encoding.UTF8.GetBytes(Data);
              req.ContentType = "application/x-www-form-urlencoded";
              req.ContentLength = postBytes.Length;
              Stream requestStream = req.GetRequestStream();
              requestStream.Write(postBytes, 0, postBytes.Length);
              requestStream.Close();
              HttpWebResponse response = (HttpWebResponse)req.GetResponse();
              Stream resStream = response.GetResponseStream();
              var sr = new StreamReader(response.GetResponseStream());
              string responseText = sr.ReadToEnd();
              WebClient client = new WebClient();
              client.UploadStringCompleted += client_UploadStringCompleted;
              client.UploadStringAsync(new Uri(url), responseText);
  }
void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
    if (e.Error != null)
    {
        // error in uploading
    }
}