c# - HttpWebResponse输出在messagebox中显示为符号

时间:2017-09-28 11:27:23

标签: c# json httpwebresponse

我目前正在尝试从HttpWebRequest收到回复并使用MessageBox显示它。我收到了fiddler节目的响应,但它在MessageBox中显示为符号。

我的问题:

我正在尝试使用MessageBox.Show函数显示响应,但响应显示为符号。我相信这是因为响应是一个json对象引起的。

包含响应结果和fiddler请求的屏幕截图:Screenshot link.

我尝试了什么:

我已经安装了Newtonsoft.Json Nuget数据包,并尝试按以下方式使用它:

var json = JsonConvert.DeserializeObject(new StreamReader(response.GetResponseStream()).ReadToEnd());
MessageBox.Show(json.ToString());

接收回复的C#代码:

try
{
    string post = "{\"input01\":{\"Input\":\"GmailAddress\",\"GmailAddress\":\"" + words[0] + "\",\"FirstName\":\"\",\"LastName\":\"\"},\"Locale\":\"de\"}";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/InputValidator?resource=SignUp&service=mail");
    request.Method = "POST";
    request.Accept = "*/*";
    request.Headers.Add("Accept-Encoding", "gzip, deflate, br");
    request.Headers.Add("Accept-Language", "en-US,en;q=0.8");
    request.Host = "accounts.google.com";
    request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36";
    request.ContentType = "application/json";
    request.KeepAlive = true;

    byte[] postBytes = Encoding.ASCII.GetBytes(post);
    request.ContentLength = postBytes.Length;
    Stream requestStream = request.GetRequestStream();

    requestStream.Write(postBytes, 0, postBytes.Length);
    requestStream.Close();

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    var html = new StreamReader(response.GetResponseStream()).ReadToEnd();
    MessageBox.Show(html);
}
catch(Exception ex)
{
    MessageBox.Show(ex.Message);
}

1 个答案:

答案 0 :(得分:0)

您正在尝试获取JSON对象的字符串。您应为此方案存储不同的字符串。

string resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
var json = JsonConvert.DeserializeObject(resp);
MessageBox.Show(resp);