0

我需要将一个字符串传递给包含一个或几个值的服务器。API语法如下:

 &track=name
OR
 &track=name&artist=name

但是服务器返回一个空白字符串。如果我删除 char &,服务器将返回如下内容:“�\b\0\0\0\0\0\0\0�ZI��F��ϯ��

            string post = "track=love";
       // post = post.Replace("&", "%26");
    //    HttpUtility.UrlEncode(post);

我应该怎么办?我应该包含 & char 还是需要从服务器读取结果?我的代码如下:

       protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        s = NavigationContext.QueryString["parameter1"];
  //      string strConnectUrl = "http://mp3.rolo.vn/mp3/Search";
        try
        {

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(strConnectUrl);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            // start the asynchronous operation
            webRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), webRequest);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
    {
        string post = "track=love"; 
        try
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            Stream postStream = request.EndGetRequestStream(asynchronousResult);

            // Convert the string into a byte array.
            byte[] postBytes = Encoding.UTF8.GetBytes(post);

            // Write to the request stream.
            postStream.Write(postBytes, 0, postBytes.Length);
            postStream.Close();

            // Start the asynchronous operation to get the response
            request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
 //      static Stream str;
    static string st;
    private static void GetResponseCallback(IAsyncResult asynchronousResult)
    { 

            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;

            // End the operation
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            HttpStatusCode rcode = response.StatusCode;
            Stream streamResponse = response.GetResponseStream(); 
            StreamReader streamRead = new StreamReader(streamResponse);

        //THIS ALWAYS RETURN "" VALUE, EXPECT TO RETURN XML STRING               
            st = streamRead.ReadToEnd();
            //Console.WriteLine(responseString);
            // Close the stream object
            streamResponse.Close();
            streamRead.Close();
            // Release the HttpWebResponse
            response.Close(); 

    }
4

2 回答 2

1

这是一个表单帖子,只需使用 & 号分隔键值:track=name&artist=name

" "�\b\0\0\0\0\0\0\0�ZI��F��ϯ��" -> 看起来结果被压缩了。

于 2011-07-21T14:41:04.823 回答
0

第一个参数应该以问号为前缀,而不是和号。后续的键值对应该用 & 号分隔。

如果你想传递一个参数,它应该如下所示:

?track=name

如果传递 2 个参数,它应该如下所示:

?track=name&artist=name
于 2011-07-21T10:24:59.820 回答