I'm working on this server side asp script.It is supposed to receive a xml stream from a web app and save it in a xml file. The problem is I'm haven't been able to read this stream. I used different methods and I can't seem to find the right one. One other thing is for testing I'm using rest console add on for chrome and it seems to work with no problems but when my client sends the stream I'm unable to read it and they receive a code 500 error.
at first I tried reading the stream in binary mode then converting it
function readContent()
dim a,b
a=Request.TotalBytes
b=Request.BinaryRead(a)
writeInLogFile(" ")
writeInLogFile(Time & Request.ServerVariables("ALL_RAW"))
writeInLogFile(Time & " Data read in binary mode with a size of " & a)
readContent = URLDecode(BytesToStr(b))
writeInLogFile(Time & " the length of the converted string is : "& len(readContent))
end function
but here's what I keep getting on my log file
17:12:10Content-Length: 8416
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Authorization: Basic
Host:
User-Agent: Jakarta Commons-HttpClient/2.0.2
17:12:10 Data read in binary mode with a size of 8416
17:12:10 Converting binary to string
and then it crashes when I try to write the converted string
then I switched to request.form
function readContent()
'writeInLogFile(Time & " " & URLDecode(Request.Form))
writeInLogFile(Time & Request.ServerVariables("ALL_RAW"))
readContent = URLDecode(Request.Form)
writeInLogFile(Time & " the length of the converted string is : "& len(readContent))
end function
but yet again when testing via rest console all is working and when actually receiving the stream from my client it just crashes.
Anyone faced a similar problem, or has an idea to how can I solve this thing
thanks in advance
update :
here is the decoding function
FUNCTION URLDecode(str)
'// This function:
'// - decodes any utf-8 encoded characters into unicode characters eg. (%C3%A5 = å)
'// - replaces any plus sign separators with a space character
'//
'// IMPORTANT:
'// Your webpage must use the UTF-8 character set. Easiest method is to use this META tag:
'// <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
'//
Dim objScript
Set objScript = Server.CreateObject("ScriptControl")
objScript.Language = "JavaScript"
URLDecode = objScript.Eval("decodeURIComponent(""" & str & """.replace(/\+/g,"" ""))")
Set objScript = NOTHING
'writeInLogFile(Time & " the length of the converted string is : "& len(URLDecode))
END FUNCTION
The encoding that client said would be sent was not the same as actually sent by the stream.
Request.Form is the answer.
Related
We have a server generated HTML file (myFile.html) that we embed in emails that get sent to our clients. We've been using this method for years with minimal issues. We use Windows Server 2012 with smtp server via II6. Recently the HTML is getting skewed in the email. When checking the source file, all looks well. Directly opening the HTML file for viewing in a browser works as you'd expect. Here is the code we're using to read the file into memory to prepare for emailing:
Set objFile = objFSO.OpenTextFile(strFilePath)
Do While objFile.AtEndOFStream <>True
line = objFile.ReadLine
If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
strHeaderWritten = "Y"
strFileContent=strFileContent & strHeader
End If
strFileContent=strFileContent & line
Loop
set objFile = Nothing
And then we add the content to the email and send:
strBody = strFileContent
Set objMail = CreateObject("CDO.Message")
Set objMail.Configuration = cdoConfig
objMail.From = strFrom
objMail.ReplyTo = strReplyTo
objMail.To = strTo
objMail.Subject = strSubject
objMail.HTMLBody = strBody
objMail.Fields("urn:schemas:httpmail:importance").Value = strImportance
objMail.Send
And here are examples of what it spits out in the email. There are no errors in the source:
Has anyone else had this happen to them?
Been toiling over this for hours looking for an explanation. Thank so much for reading!
I tried using the ADO Stream method for the email, but it is still coming out the same:
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 2 'adTypeText
objStream.CharSet = Application("CharacterSet")
objStream.Open
objStream.LoadFromFile strFilePath
Do While Not objStream.EOS
line = objStream.ReadText(-2)
If Instr(1, line, "<table") > 0 And strHeaderWritten = "N" Then
strHeaderWritten = "Y"
strFileContent=strFileContent & strHeader
End If
If Instr(1, line, "< table") > 0 Then
strFileContent=strFileContent & "<h3>Broken HTML</h3>"
End If
strFileContent=strFileContent & line
Loop
objStream.Close
Set objStream = Nothing
As you can see, I also added a check for one of the persistent errors I'm seeing where there has been a space inserted between < and table. Checking the output this way did not capture the issue as in checking the text for the added space. So it must be happening after it's been written or I need to use a regex for the test. I'll try that next. I'm still seeing it in multiple email clients. Here's an example post test of ADO Stream:
This seems to be a common problem in CDO. I've found a few references online to the problem that spaces are randomly inserted into the HTMLbody.
One answer was to make the HTML body not one long string, because CDO will then insert random spaces, but to include whitespace yourself, so that CDO doesn't have to.
You could try adding VbCrLf or just plain spaces in the text you're sending.
A second suggestion made more sense to me; this can be an encoding problem. That also explains why adding your own whitespace could be a workaround.
Anyway; CDO allows for setting the encoding of the CDO.Message object before sending.
Try objMail.BodyPart.ContentTransferEncoding = "quoted-printable" to see if that solves it.
The issue is windows use of both line break and carrage return. I recommend loading the body of the text and replacing all instances of vbcrlf with just vblf and you will find you wont have the double spacing anymore.
e.g.
body = replace(body, vbcrlf, vblf)
How can I show an nvarchar column that stores unicode data (Entered with the zawgyi1 font) in a classic ASP web page?
When I retrieve and write the value to the page, it shows "?????". I set my ASP page's content type of UTF-8 with the following meta tag:
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
Unfortunately, the text is still rendered as "?????".
Any suggestions or ideas on how to display unicode values in a classic ASP page?
The Content-Type meta header informs the browser to treat the content sent as a UTF-8 encoded text stream. It doesn't ensure that the stream sent is actually UTF-8. To handle UTF-8 correctly you need to do 3 things:-
Ensure your static content is saved in a UTF-8 compatible encoding.
Ensure your dynamic content is encoded to UTF-8.
Inform the client that the content is UTF-8 encoded.
Item 1 requires either that you actually save the ASP file as a UTF-8 encoded file or that all your static content in the file is within the ASCII character range (0-127). Note if you save as UTF-8 then all your server-side script must use characters within the ASCII character range. In Visual Studio you can do so by "Saving the file AS..." and then clicking on the little arrow on the Save button.
Item 2 requires that the Response.CodePage property is set to the UTF-8 code page 65001, you can do this in code or by adding the attribute CODEPAGE=65001 to the <%# %> declarations on the first line of the ASP file. If you do it in code you must set it before any calls to Response.Write.
AND: do not use chr or asc functions (these are buggy when using 65001) but use chrw and ascw instead.
Item 3 requires that the Content-Type header contains the charset=UTF-8 qualifier. As you are already doing you can do this with the META header. Personally I find that to be a bit of kludge, I prefer to use Response.Charset = "UTF-8" in code. This places the qualifier on the true Content-Type HTTP header.
What about your codepage definition at the top of your page?
<%#LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
Here's a useful script to batch-convert ASP files from ANSI to UTF-8 encoding:
<HTML>
<HEAD>
<TITLE>ASP UTF-8 Converter - TFI 13/02/2015</TITLE>
</HEAD>
<BODY style='font-face:arial;font-size:11px'>
<%
Dim fso, folder, files, NewsFile, sFolder, objFSO, strFileIn, strFileOut
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "C:\inetpub\wwwroot\sitefolder"
Function ANSItoUTF8( ANSIFile)
UFT8FileOut=ANSIFile&".utf8"
Set oFS = CreateObject( "Scripting.FileSystemObject" )
Set oFrom = CreateObject( "ADODB.Stream" )
sFFSpec = oFS.GetAbsolutePathName(ANSIFile)
Set oTo = CreateObject( "ADODB.Stream" )
sTFSpec = oFS.GetAbsolutePathName(UFT8FileOut)
oFrom.Type = 2 'adTypeText
oFrom.Charset = "Windows-1252"
oFrom.Open
oFrom.LoadFromFile sFFSpec
oTo.Type = 2 'adTypeText
oTo.Charset = "utf-8"
oTo.Open
oTo.WriteText oFrom.ReadText
oTo.SaveToFile sTFSpec,2
oFrom.Close
oTo.Close
oFS.DeleteFile sFFSpec
oFS.MoveFile sTFSpec,sFFSpec
End Function
ConvertFiles fso.GetFolder(sFolder), True
Function ConvertFiles(objFolder, bRecursive)
Dim objFile, objSubFolder
For each objFile in objFolder.Files
If Ucase(fso.GetExtensionName(objFile)) = "ASP" Then
ANSItoUTF8 objFile.path
response.write "• Converted <B>"&fso.GetAbsolutePathName(objFile)&"</B> from ANSI to UTF-8<BR>"
End If
Next
If bRecursive = true then
For each objSubFolder in objFolder.Subfolders
ConvertFiles objSubFolder, true
Next
End If
End Function
%>
</BODY>
</HTML>
This question is related to this one: Character encoding Microsoft.XmlHttp in Vbscript, but differs in one thing, the national characters are in the domain name, not only arguments.
The task is: download a page from the given URL.
I already solved problem of passing UTF8 string into VBScript by reading it from UTF8 encoded file through ADO.
But now when I try opening it MSXML2.ServerXMLHTTP returns error: The URL is invalid.
Here is VBScript code:
Set objStream = CreateObject("ADODB.Stream")
objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile("fileWithURL.txt")
url = objStream.ReadText()
objStream.Close
Set XMLHttpReq = CreateObject("MSXML2.ServerXMLHTTP")
XMLHttpReq.Open "GET", url, False
XMLHttpReq.send
WEBPAGE = XMLHttpReq.responseText
If you put something like hxxp://россия.рф/main/page5.html into the UTF8 encoded fileWithURL.txt the script will raise an error while working ok with hxxp://google.com.
The workaround is to use ascii representation of the domain name - but I yet haven't found PunnyCode encoder for vbscript (apart from Chillkat which is an overkill for my task).
Will appreciate your help on the main problem or workaround.
I've made an amazing journey in to depth of my hard drive and found a code writen by / for Jesper Høy. This was the source code of SimpleDNS Plus' IDN Conversion Tool at that time.
Archive.org page snapshot: http://www.simpledns.com/idn-convert.asp
Archive.org file snapshot: idn-convert-asp.zip
You can also copy the whole code from this gist.
Create a function to convert URLs.
Function DummyPuny(ByVal url)
Dim rSegments : rSegments = Split(url, "/")
If UBound(rSegments) > 1 Then
rSegments(2) = DomainPunyEncode(rSegments(2))
End If
DummyPuny = Join(rSegments, "/")
End Function
Then convert your url before making the request.
XMLHttpReq.Open "GET", DummyPuny(url), False
I am using Gmail SMTP server to send out a .mht file. once send out, i am getting a lot of attachemnts and the .mht file were loading in the email body (from yahoo mail). Instead in outlook, i am getting it as a mail attachements.
VB script used for this :
Set objMessage = CreateObject("CDO.Message")
objMessage.HTMLBody = "<h1>Matrikon AM</h1>"
objMessage.Subject = "Test 1 -Blank"
objMessage.From = "xxxxx#gmail.com"
objMessage.To = "xxxxx#yahoo.com"
objMessage.TextBody = "From b.vbs"
objMessage.AddAttachment "c:\xxxxxxx\A1.mht"
objMessage.Send
I had some quick search online that "ContentMediaType" have to be define somewhere since the file extension is .mht.
Need help on this as i could not find a way to define the content-type.
My issue were exactly like mentioned in "https://stackoverflow.com/questions/15976836/modifying-the-content-type-of-an-attachment-in-a-cdo-message-object"
thanks.
This is from Help.
urn:schemas:mailheader: Namespace
The urn:schemas:mailheader: namespace defines fields that contain Internet standard message header values. Each field value (with a few exceptions) is stored as US-ASCII characters and is identical to the ASCII string found in the message stream. Non US-ASCII characters are encoded according to the RFC 1522 specification. No conversion is performed when the property value is set or updated. An application that sets the raw message header property must RFC 1522-encode non US-ASCII characters or the header value will be corrupted.
String constants are provided in the C++ header file cdosysstr.h and type library (cdoMailHeader module) for each field name. You can use these constants when referring to the fields to avoid typos and extra typing.
Example
The following example demonstrates use of the fields in the urn:schemas:mailheader: namespace. First, RFC 822 headers are added for a message. Next, body parts are added and the MIME headers set manually.
This code is for illustrative purposes only.
Copy Code
Dim iMsg as New CDO.Message
Dim Flds as ADODB.Fields
With iMsg
.To = """Someone"" <example#example.com>"
.From = """Me"" <example#example.com>"
.Subject = "Here is a sample message"
' Now set some custom mail headers using the raw fields collection
Set Flds = .Fields
With Flds
.Item("urn:schemas:mailheader:X-Mailer") = "Microsoft CDO for Windows 2000"
' I add a custom header here
.Item("urn:schemas:mailheader:Myheader")= "some value"
.Update
.Resync
End With ' Flds
End With ' iMsg
' Create a multipart/alternative (HTML) message below
Dim iBp as CDO.IBodyPart
Dim iBp2 as CDO.IBodyPart
Set iBp = iMsg ' get IBodyPart on Message object
Set Flds = iBp.Fields
Flds("urn:schemas:mailheader:content-type") = "multipart/alternative"
Flds.Update
Set iBp2 = iBp.AddBodyPart
Set Flds = iBp2.Fields
Flds("urn:schemas:mailheader:content-type") = "text/plain"
Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
Flds.Update
Dim Stm as ADODB.Stream
Set Stm = iBp2.GetDecodedContentStream
Stm.WriteText "This is a test", stWriteLine
Stm.Flush
Set iBp2 = iBp.AddBodyPart
Set Flds = iBp2.Fields
Flds("urn:schemas:mailheader:content-type") = "text/html"
Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
Flds.Update
Set Stm = iBp2.GetDecodedContentStream
Stm.WriteText "This is a <i>test</i>", stWriteLine
Stm.Flush
iMsg.Send
I upsized an Access 2007 database to SQL Server 2008 R2. The images are in SQL Server as image type. Access has link to the table containing the image. When I try to display from within Access, it won't do it. It still has the OLE Object wrapper.
How can I get that image and display it on my forms in Access? I do not have the option, at the moment, to remove the images, put them in a directory and point to them (the best way I know but not an option). I need to read the image / blob file directly from SQL Server and display it on an Access form.
Thank you for any ideas.
I saw this but it did not help:
How to display image from sql server in ms access
http://access.bukrek.net/documentation looks like the file in folder method
Since Access 2010, you can use the PictureData property to store and display images from SQL Server. You will need a bound control for an SQL Server data type varbinary(max), which can be hidden, and an unbound Image control in MS Access. You can now simply say:
Private Sub Form_Current()
Me.MSAccessImageControl.PictureData = Me.SQLServerImage
End Sub
And vice versa. You will need to add some error management to that, but very little else.
Below is a function I have successfully used called BlobToFile. And I also posted the code that I use to test it. The picture gets dumped to a so-called temp file but its not truly temp because it isn't in the temp directory. You can manually delete the image file or else you'll have to write it to your temp folder instead. Then I have an image control where I display the picture.
Private Sub Command1_Click()
Dim r As DAO.Recordset, sSQL As String, sTempPicture As String
sSQL = "SELECT ID, PictureBlobField FROM MyTable"
Set r = CurrentDb.OpenRecordset(sSQL, dbSeeChanges)
If Not (r.EOF And r.BOF) Then
sTempPicture = "C:\MyTempPicture.jpg"
Call BlobToFile(sTempPicture, r("PictureBlobField"))
If Dir(sTempPicture) <> "" Then
Me.imagecontrol1.Picture = sTempPicture
End If
End If
r.Close
Set r = Nothing
End Sub
'Function: BlobToFile - Extracts the data in a binary field to a disk file.
'Parameter: strFile - Full path and filename of the destination file.
'Parameter: Field - The field containing the blob.
'Return: The length of the data extracted.
Public Function BlobToFile(strFile As String, ByRef Field As Object) As Long
On Error GoTo BlobToFileError
Dim nFileNum As Integer
Dim abytData() As Byte
BlobToFile = 0
nFileNum = FreeFile
Open strFile For Binary Access Write As nFileNum
abytData = Field
Put #nFileNum, , abytData
BlobToFile = LOF(nFileNum)
BlobToFileExit:
If nFileNum > 0 Then Close nFileNum
Exit Function
BlobToFileError:
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, _
"Error writing file in BlobToFile"
BlobToFile = 0
Resume BlobToFileExit
End Function