我需要将一个字符串传递给包含一个或几个值的服务器。 API语法如下:
&track=name
OR
&track=name&artist=name
但是服务器返回一个空字符串。 如果我删除char&amp ;,服务器将返回如下内容: “\ B \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \0ZIFϯ
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();
}
答案 0 :(得分:1)
这是一个表单帖子,只需使用&符号来分隔键值: 轨道=名&安培;艺术家名=
“” \ b \ 0 \ 0 \ 0 \ 0 \ 0 \ 0 \0 ZI F ϯ “ - >看起来结果已被压缩。
答案 1 :(得分:0)
第一个参数应以问号为前缀,而不是&符号。随后的键值对应用&符分隔。
如果要传递一个参数,它应该如下所示:
?track=name
如果传递2个参数,它应该是:
?track=name&artist=name