How to convert Office 365 �ɂ����鑼�̃��[�U�[�̃��[���{�b�N�X�ւ̃A�N�Z�X�Ɋւ���K�C�h�`���`���[�g���A��

StackOverflow https://stackoverflow.com/questions/23691000

  •  29-07-2023
  •  | 
  •  

Question

I am reading source of the following url but the title is coming as bunch of ?? marks, how do I convert it to actual language that the web page is presenting.

http://support.microsoft.com/common/survey.aspx?scid=sw;ja;3703&showpage=1

private string[] getTitleNewUrl()
{   
    string url = @"http://support.microsoft.com/common/survey.aspx?scid=sw;ja;3703&showpage=1";
    string[] titleNewUrl = new string[2];
    var navigatedUrl = string.Empty;

    string title = string.Empty;
    try
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Credentials = System.Net.CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        if (response.StatusCode == HttpStatusCode.OK)
        {
            navigatedUrl = response.ResponseUri.ToString(); **//this returns [http://support.microsoft.com/default.aspx?scid=gp;en-us;fmserror][1]**

            StreamReader sr = new StreamReader(response.GetResponseStream());
            var htmlSource = sr.ReadToEnd();

            Match m = Regex.Match(htmlSource, @"<title>\s*(.+?)\s*</title>");
            if (m.Success)
            {
                title = m.Groups[1].Value;
            }

            titleNewUrl[0] = title;
            titleNewUrl[1] = navigatedUrl;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Invalid URL: " + navigatedUrl + " Error: " + ex.Message);
    }

    return titleNewUrl;
}

Thanks

Était-ce utile?

La solution

Here is the answer

public string GetResponseStream(string sURL)
    {
        string strWebPage = "";
        // create request
        System.Net.WebRequest objRequest = System.Net.HttpWebRequest.Create(sURL);
        // get response
        System.Net.HttpWebResponse objResponse;
        objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();
        // get correct charset and encoding from the server's header
        string Charset = objResponse.CharacterSet;
        Encoding encoding = Encoding.GetEncoding(Charset);
        // read response
        using (StreamReader sr = new StreamReader(objResponse.GetResponseStream(), encoding))
        {
            strWebPage = sr.ReadToEnd();
            // Close and clean up the StreamReader
            sr.Close();
        }

        return strWebPage;
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top