将 & 字符发送到 HttpWebRequest
我需要将包含一个或几个值的字符串传递到服务器。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个表单帖子,只需使用 & 来分隔键值:
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.
第一个参数应以问号开头,而不是与符号。后续的键值对应该用 & 符号分隔。
如果您想传递一个参数,则应如下所示:
如果传递 2 个参数,则应如下所示:
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:
If passing 2 parameters it should look like: