In .net I am using a HttpWebRequest to read from a WebSite. I am getting
everything back except for some characters above hex 7F which appear to have
been stripped out of my response. I see these characters if I examine the
site with IE.
It has been suggested that this is an encoding problem, but I'm unsure as
what I need to do about it. Can anybody help? 3 1837
Hi David,
This does indeed look like an encoding problem. The WebSite probably does
not use UTF-8 which is the default encoding for the StreamReader used by
GetResponseStre am. If there is no encoding information in the headers you
should be able to find the information somewhere in the data itself.
Check WebResponse.Con tentEncoding or if no encoding is set look for
'charset' in the source code.
You can download the data using UTF-8 then convert it to a bytestream and
back to a string using the new encoding.
--
Happy Coding!
Morten Wennevik [C# MVP]
It certainly sounds as though you know what you are talking about, however I
need more help.
The WebSite talks OK to IE just not to my C# .net program.
I checked the WebResponse with a quick watch and the stated encoding was "".
Can you tell me more precisely what coding changes I need to make to my code
to get it to work. I tried setting the TransferEncodin g but that just caused
an error in Get Response.
My current coding snippet for getting the URL response is as follows.
private String ReadURL()
{
HttpWebRequest reqURL =
(HttpWebRequest )WebRequest.Cre ate(ToString()) ;
reqURL.Credenti als = CredentialCache .DefaultCredent ials;
HttpWebResponse respURL = (HttpWebRespons e)reqURL.GetRes ponse();
Stream streamURL = respURL.GetResp onseStream();
return (new StreamReader(st reamURL)).ReadT oEnd();
}
This is a piece of code I used to handle encodings not found in the
response stream.
Stream s = resp.GetRespons eStream();
byte[] buffer = ReadStream(s); // ReadStream reads the Stream into a byte[]
// time to check encoding
string urlEnc = resp.ContentEnc oding;
Encoding e = null;
if(urlEnc.Lengt h > 0)
e = Encoding.GetEnc oding(urlEnc);
else
e = Encoding.UTF8;
string temp = e.GetString(buf fer, 0, buffer.Length);
// in case if no encoding, redecode the page
if(resp.Content Encoding.Length == 0)
{
string charset = GetCharSet(resp .ContentType, true);
if(charset == null)
charset = GetCharSet(temp , false);
if(charset != null)
temp = Encoding.GetEnc oding(charset). GetString(buffe r, 0, buffer.Length);
}
....
// the idea of getcharset is to look for the charset tag in the source
// I forgot why all the details, but those are probably to ensure all
manners of writing will be detected
private static string GetCharSet(stri ng s, bool header)
{
try
{
int i = s.IndexOf("char set"); // try lower case first
if(i == -1)
i = s.IndexOf("CHAR SET");
if(i == -1) // charset not found, return
return null;
int j = s.IndexOf("=", i+1);
if(j == -1)
return null;
if(header)
{
int n = s.IndexOf(";", j+1);
if(n == -1)
return s.Substring(j+1 );
else
return s.Substring(j+1 , n-(j+1));
}
int k = s.IndexOf("\"", j+1);
int l = s.IndexOf(">", j+1);
int m = s.IndexOf("'", j+1);
if(k == -1 && l == -1 && m == -1) // not able to detect end of the
encoding word
return null;
if(k == -1)
k = Int32.MaxValue;
if(l == -1)
l = Int32.MaxValue;
if(m == -1)
l = Int32.MaxValue;
if(k == Int32.MaxValue)
return null;
// the previous eight lines are probably obsolete code I forgot to remove
// if k == -1 the substring wouldn't work
string temp = s.Substring(j+1 , k-j-1);
if(temp.Length == 0)
return null;
else
return temp;
}
catch(Exception ex)
{
MessageBox.Show ("GetCharSet Error: " + ex.Message);
return null;
}
}
--
Happy Coding!
Morten Wennevik [C# MVP] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ann |
last post by:
Hi,
Is there any way to Change encoding of Java Vm to ISO-8859-1?
i am using Java vm along with an application called opencms.
I get the following error message..
Error: the encoding of your Java VM is different from
the OpenCms encoding!
Java VM file encoding: UTF-8
|
by: janeaustine50 |
last post by:
Python's InteractiveInterpreter uses the built-in compile function.
According to the ref. manual, it doesn't seem to concern about the
encoding of the source string.
When I hand in an unicode object, it is encoded in utf-8 automatically.
It can be a problem when I'm building an interactive environment using
"compile", with a different encoding from utf-8. IDLE itself has the
same problem. ( '<a string with non-ascii-encoding>' is...
|
by: Mark |
last post by:
Hi...
I've been doing a lot of work both creating and consuming web services, and
I notice there seems to be a discontinuity between a number of the different
cogs in the wheel centering around windows-1252 and that it is not equivalent
to iso-8859-1.
Looking in the registry under HKEY_CLASSES_ROOT\MIME\Database\Charset and
\Codepage, it seems that all variations on iso-8859-1 (latin1, etc) are
mapped to code page 1252, which I'm...
|
by: davisjoseph |
last post by:
Hi All,
I'm newbie to this XML world. My problem is to identify the encoding
type of XML at runtime. What currently I'm doing is checking whether
BOM is available in the XML; based on the BOM I'm identifying the
encoding type. Here is the problem, some type of UTF-8 encoded file
does'nt have BOM in the starting. So I'm identying the file as
iso-8859-1 encoded which is actually encoded in UTF-8.
I dont have much idea about the...
|
by: Demon News |
last post by:
I'm trying to do a transform (Using XmlTransform class in c#) and in the
Transform I'm specifying the the output xsl below:
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
the resulting xml ends up with the following declaration:
<?xml version="1.0" encoding="utf-16"?>
changing the encoding to utf-16, is there something I'm doing wrong? Is it
| |
by: fitsch |
last post by:
Hi,
I am trying to write a generic RSS/Atom/OPML feed client. The problem
is, that those xml feeds may have different encodings:
- <?xml version="1.0" encoding="ISO-8859-1" ?>...
- <?xml version="1.0" encoding="utf-8" ?>...
- ...
I am using the WebRequest functionality to get the feeds. So, my code
|
by: James Wong |
last post by:
Dear all,
I've a web service function and it contains a parameter in
System.Text.Encoding. I found that the data type of this parameter in
caller application becomes MyWebSvcName.Encoding (where "MyWebSvcName" is
the name of my web service). I don't know how to assign value to this
parameter. (I'm using VB.NET 2003.)
Here are some sample codes:
|
by: Janusz Nykiel |
last post by:
I've stumbled upon unexpected behavior of the .NET 2.0 System.Xml.XmlWriter
class when using it to write data to a binary stream (System.IO.Stream). If
the amount of data is less than a certain value (which varies depending on
the data being written), characters not available in the encoding specified
in the Encoding property of the XmlWritterSettings instance used to create
the XmlWriter are being written to the resulting XML document as...
|
by: Martin Z |
last post by:
Hi,
I have an application that involves sending a lot of XML data to
various places. The problem is that once in a while, I just want the
XML document as a string (for example, sending to a typed dataset
tableadaptor). In that case, I have a problem: what encoding do I use
to conver the byte (or memorystream) into a string? The encoding is
in the file, of course, like any XML file, but how do I find out
which? I tried using an...
|
by: Allan Ebdrup |
last post by:
I hava an ajax web application where i hvae problems with UTF-8 encoding oc
chineese chars.
My Ajax webapplication runs in a HTML page that is UTF-8 Encoded.
I copy and paste some chineese chars from another HTML page viewed in IE7,
that is also UTF-8 encoded (search for "china" on google.com). I paste the
chineese chars into a content editable div.
My Ajax webservice compiles an XML where the data from the content editable
div is...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |