我需要将一个字符串传递给包含一个或几个值的服务器。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();
}