将 & 字符发送到 HttpWebRequest

发布于 2024-11-25 06:15:25 字数 2930 浏览 1 评论 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);

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

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

    }

I need to pass a string to server that contains one or few value. The API grammar is as following:

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

however server return a blank string.
if I remove the char &, server will return some thing like this:
"�\b\0\0\0\0\0\0\0�ZI��F��ϯ��

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

What should I do? should I have the & char included or need to read the result from server?
My code is as follow:

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

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦途 2024-12-02 06:15:25

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

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

It's a form post, just use ampersand to separate key values:
track=name&artist=name

" "�\b\0\0\0\0\0\0\0�ZI��F��ϯ��" -> looks like the result is compressed.

箹锭⒈辈孓 2024-12-02 06:15:25

第一个参数应以问号开头,而不是与符号。后续的键值对应该用 & 符号分隔。

如果您想传递一个参数,则应如下所示:

?track=name

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

?track=name&artist=name

The first parameter should be prefixed with a question mark, not an ampersand. Subsequent key value pairs shoudl be separated with an ampersand.

If you want to pass one parameter it should look like:

?track=name

If passing 2 parameters it should look like:

?track=name&artist=name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文