我需要传递一个字符串到服务器,其中包含一个或几个值。API语法如下:
&track=name
OR
&track=name&artist=name
然而,服务器返回一个空字符串。如果我删除字符&,服务器将返回类似这样的东西:"� b 0 0 0 0�子��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();
}
这是一个表单帖子,只是使用&来分隔键值:跟踪= name&艺术家名字=
"� b 0 0 0 0�子��F��ϯ��"——>看起来像被压缩的结果。
第一个参数的前缀应该是问号,而不是&。后续的键值对应该用"&"分隔。
如果你想传递一个参数,它应该看起来像:
?track=name
如果传递两个参数,它应该是这样的:
?track=name&artist=name