Nghia Nguyen
Nghia Nguyen

Reputation: 2665

Post ampersand character to HttpWebRequest

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

    }

Upvotes: 0

Views: 1080

Answers (2)

bbaia
bbaia

Reputation: 1183

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.

Upvotes: 1

Matt Lacey
Matt Lacey

Reputation: 65586

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

Upvotes: 0

Related Questions