|
Try adding this segment to Web.comfig file. It worked for me.
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
...
</configuration>
It will instruct your app to parse malformed headers. You can find more on this at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=21106&SiteID=1&PageID=0
Dimitar
|
|
|
|
|
Thank You very much
It worked for me..
Thanks Again....
|
|
|
|
|
I tried to put that in my app.config file, but it doesn't help. I get the same "There was a problem connecting to the Babelfish server" error.
I am doing this in a Windows Application. Is there something else I need to do for that?
Thanks for your time and help!
Brenda
|
|
|
|
|
This code sets the content-length header of the request to the number of characters in the string. Instead, it should be the number of bytes in the request body (the length of the "bytes" array). It works for English because all the characters get encoded as one byte, but may not work for other languages if Babel Fish is paying attention to this header. It may be ignoring this header in which case it doesn't matter, but this should probably be fixed at some point.
|
|
|
|
|
Hi!
How can I use the same feature using VC++ development environment.
Regards,
Milind
|
|
|
|
|
See this[^] article.
/ravi
|
|
|
|
|
This is a great article but I am unable to connect to Babelfish and everyone seems to be in agreement to use Google. I have made what I think to be the correct changes to use Google but I have not had any luck. Can you provide the source for the C# method that will connect to Google?
Thanks,
Matt
|
|
|
|
|
|
First off, thanks Matt. Works great!!
I tested both Google & Babelfish and both work fine however in the long term, which is the better one to go with for a website (i.e. more stable..less likely to alter the parameters in their URL, etc)?
Is there one that's better than the other?
|
|
|
|
|
I went with Babelfish, but I'm switching over to Google. We have some strings over the 150 limit. I think both versions claim they won't handle over 150, but I just tried Google and it doesn't seem to throw an exception. I'm in the same situation, curious what your experience has been.
Steve MunLeeuw
|
|
|
|
|
Thanks for the tip on the 150 character limit Steve. I wasn't aware of that. I've actually been using google for content containing over 600 characters and it comes back fine.
I tested my app w/Babelfish in the beginning but only with a few words at a time. I guess I'll be using google from here on out.
|
|
|
|
|
After messing around with this example.
The original code was:
postsourcedata = "lp=" + translationmode + "&tt=urltext&intl=1&doit=done&urltext=" + HttpUtility.UrlEncode(sourcedata);
And in the javascript on the site is different, so I changed the code to reflect the following:
postsourcedata = "doit=done&intl=1&tt=urltext&trtext=" + HttpUtility.UrlEncode(sourcedata) + "&lp=" + HttpUtility.UrlEncode(translationmode);
Also make the following regex is the same:
Regex reg = new Regex(@"<div style=padding:10px;>((?:.|\n)*?)</div>", RegexOptions.IgnoreCase);
I also made sure I added the code in the web.config from a previous post:
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net></code>
And it should work, finally.
Campbell
-- modified at 23:15 Sunday 15th January, 2006
|
|
|
|
|
Imports System.Web.Services
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Diagnostics
Imports System.Web
Imports System.Net
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.IO
<WebService(Namespace:="http://www.redgreenyellowbluegreenpinkpurplewhite.com/Translation/")> _
Public Class Translate
Inherits System.Web.Services.WebService
Public Sub New()
MyBase.New()
'This call is required by the Web Services Designer.
InitializeComponent()
'Add your own initialization code after the InitializeComponent() call
End Sub
'Required by the Web Services Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Web Services Designer
'It can be modified using the Web Services Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
'CODEGEN: This procedure is required by the Web Services Designer
'Do not modify it using the code editor.
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private Const ERRORSTRINGSTART As String = "<font color=red>"
Private Const ERRORSTRINGEND As String = "</font>"
<WebMethod(Description:="zh_en=Chinese-simp to English, zt_en=Chinese-trad to English, en_zh=English to Chinese-simp, " & _
" en_zt=English to Chinese-trad, en_nl=English to Dutch, en_fr=English to French, en_de=English to German, en_el=English to Greek" & _
" en_it=English to Italian, en_ja=English to Japanese, en_ko=English to Korean, en_pt=English to Portuguese, en_ru=English to Russian," & _
" en_es=English to Spanish, nl_en=Dutch to English, nl_fr=Dutch to French,fr_nl=French to Dutch, fr_en=French to English, fr_de=French to German, fr_el=French to Greek," & _
" fr_it=French to Italian, fr_pt=French to Portuguese, fr_es=French to Spanish, de_en=German to English, de_fr=German to French, el_en=Greek to English, el_fr=Greek to French, it_en=Italian to English," & _
" it_fr=Italian to French, ja_en=Japanese to English, ko_en=Korean to English, pt_en=Portuguese to English, pt_fr=Portuguese to French, ru_en=Russian to English, es_en=Spanish to English, es_fr=Spanish to French")> _
Public Function BabelFish(ByVal translationmode As String, ByVal sourcedata As String) As String
Dim VALIDTRANSLATIONMODES() As String = New String() {"zh_en", "zt_en", "en_zh", "en_zt", "en_nl", "en_fr", "en_de", "en_el", "en_it", "en_ja", "en_ko", "en_pt", "en_ru", "en_es", "nl_en", "nl_fr", "fr_en", "fr_de", "fr_el", "fr_it", "fr_pt", "fr_nl", "fr_es", "de_en", "de_fr", "el_en", "el_fr", "it_en", "it_fr", "ja_en", "ko_en", "pt_en", "pt_fr", "ru_en", "es_en", "es_fr"}
Dim SITEURL As String = "http://babelfish.altavista.com/babelfish/tr"
Dim SITEREFERER As String = "http://babelfish.altavista.com/"
Try
' validate and remove trailing spaces
If ((translationmode = Nothing) _
OrElse (translationmode.Length = 0)) Then
Throw New ArgumentNullException("translationmode")
End If
If ((sourcedata = Nothing) _
OrElse (translationmode.Length = 0)) Then
Throw New ArgumentNullException("sourcedata")
End If
translationmode = translationmode.Trim
sourcedata = sourcedata.Trim
' check for valid translationmodes
Dim validtranslationmode As Boolean = False
Dim i As Integer = 0
Do While (i < VALIDTRANSLATIONMODES.Length)
If (VALIDTRANSLATIONMODES(i) = translationmode) Then
validtranslationmode = True
End If
i = (i + 1)
Loop
If Not validtranslationmode Then
Return (ERRORSTRINGSTART + ("ERROR1:The translation mode specified was not a valid translation translation mode" + ERRORSTRINGEND))
End If
Dim uri As Uri = New Uri(SITEURL)
Dim request As HttpWebRequest = CType(WebRequest.Create(uri), HttpWebRequest)
request.Referer = SITEREFERER
' Encode all the sourcedata
Dim postsourcedata As String
postsourcedata = ("lp=" _
+ (translationmode + ("&tt=urltext&intl=1&doit=done&trtext=" + HttpUtility.UrlEncode(sourcedata))))
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = postsourcedata.Length
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
Dim writeStream As Stream = request.GetRequestStream
Dim encoding As UTF8Encoding = New UTF8Encoding
Dim bytes() As Byte = encoding.GetBytes(postsourcedata)
writeStream.Write(bytes, 0, bytes.Length)
writeStream.Close()
Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
Dim responseStream As Stream = response.GetResponseStream
Dim readStream As StreamReader = New StreamReader(responseStream, encoding.UTF8)
Dim page As String = readStream.ReadToEnd
Dim reg As Regex = New Regex("<div style=padding:10px;>((?:.|\n)*?)</div>", RegexOptions.IgnoreCase)
Dim matches As MatchCollection = reg.Matches(page)
If ((matches.Count <> 1) _
OrElse (matches(0).Groups.Count <> 2)) Then
Return (ERRORSTRINGSTART + ("ERROR2:The HTML returned from Babelfish appears to have changed. Please check for an updated regular express" & _
"ion" + ERRORSTRINGEND))
End If
Return matches(0).Groups(1).Value
Catch ex As ArgumentNullException
Return (ERRORSTRINGSTART _
+ (ex.Message + ERRORSTRINGEND))
Catch ex As ArgumentException
Return (ERRORSTRINGSTART _
+ (ex.Message + ERRORSTRINGEND))
Catch ex As WebException
Return (ERRORSTRINGSTART + ex.Message + ("ERROR3:There was a problem connecting to the Babelfish server" + ERRORSTRINGEND))
Catch ex As System.Security.SecurityException
Return (ERRORSTRINGSTART + ("ERROR4:You do not have permission to make HTTP connections. Please check your assembly's permission s" & _
"ettings" + ERRORSTRINGEND))
Catch ex As Exception
Return (ERRORSTRINGSTART + ("ERROR5:An unspecified error occured: " _
+ (ex.Message + ERRORSTRINGEND)))
End Try
End Function
<WebMethod(Description:="en|de=English to German, en|es=English to Spanish, en|fr=English to French, en|it=English to Italian, en|pt=English to Portuguese, en|ja=English to Japanese BETA, en|ko=English to Korean BETA, en|zh-CN=English to Chinese(Simplified) BETA, de|en=German to English, de|fr=German to French, es|en=Spanish to English, fr|en=French to English, fr|de=French to German, it|en=Italian to English, pt|en=Portuguese to English, ja|en=Japanese to English BETA, ko|en=Korean to English BETA, zh-CN|en=Chinese (Simplified) to English BETA")> _
Public Function GoogleTranslate(ByVal translationmode As String, ByVal sourcedata As String) As String
Dim VALIDTRANSLATIONMODES() As String = New String() {"en|de", "en|es", "en|fr", "en|it", "en|pt", "en|ja", "en|ko", "en|zh-CN", "de|en", "de|fr", "es|en", "fr|en", "fr|de", "it|en", "pt|en", "ja|en", "ko|en", "zh-CN|en"}
Dim SITEURL As String = "http://translate.google.com/translate_t"
Dim SITEREFERER As String = "http://translate.google.com/"
Try
' validate and remove trailing spaces
If ((translationmode = Nothing) _
OrElse (translationmode.Length = 0)) Then
Throw New ArgumentNullException("translationmode")
End If
If ((sourcedata = Nothing) _
OrElse (translationmode.Length = 0)) Then
Throw New ArgumentNullException("sourcedata")
End If
translationmode = translationmode.Trim
sourcedata = sourcedata.Trim
' check for valid translationmodes
Dim validtranslationmode As Boolean = False
Dim i As Integer = 0
Do While (i < VALIDTRANSLATIONMODES.Length)
If (VALIDTRANSLATIONMODES(i) = translationmode) Then
validtranslationmode = True
End If
i = (i + 1)
Loop
If Not validtranslationmode Then
Return (ERRORSTRINGSTART + ("ERROR1:The translation mode specified was not a valid translation translation mode" + ERRORSTRINGEND))
End If
Dim uri As Uri = New Uri(SITEURL)
Dim request As HttpWebRequest = CType(WebRequest.Create(uri), HttpWebRequest)
request.Referer = SITEREFERER
' Encode all the sourcedata
Dim postsourcedata As String
postsourcedata = ("langpair=" _
+ (translationmode + ("&text=" + HttpUtility.UrlEncode(sourcedata))))
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = postsourcedata.Length
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
Dim writeStream As Stream = request.GetRequestStream
Dim encoding As UTF8Encoding = New UTF8Encoding
Dim bytes() As Byte = encoding.GetBytes(postsourcedata)
writeStream.Write(bytes, 0, bytes.Length)
writeStream.Close()
Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
Dim responseStream As Stream = response.GetResponseStream
Dim readStream As StreamReader = New StreamReader(responseStream, encoding.UTF8)
Dim page As String = readStream.ReadToEnd
'<textarea name=q rows=5 cols=45 wrap=PHYSICAL>maison</textarea>
Dim reg As Regex = New Regex("<textarea name=q rows=5 cols=45 wrap=PHYSICAL>((?:.|\n)*?)</textarea>", RegexOptions.IgnoreCase)
Dim matches As MatchCollection = reg.Matches(page)
If ((matches.Count <> 1) _
OrElse (matches(0).Groups.Count <> 2)) Then
Return (ERRORSTRINGSTART + ("ERROR2:The HTML returned from Google appears to have changed. Please check for an updated regular express" & _
"ion" + ERRORSTRINGEND))
End If
Return matches(0).Groups(1).Value
Catch ex As ArgumentNullException
Return (ERRORSTRINGSTART _
+ (ex.Message + ERRORSTRINGEND))
Catch ex As ArgumentException
Return (ERRORSTRINGSTART _
+ (ex.Message + ERRORSTRINGEND))
Catch ex As WebException
Return (ERRORSTRINGSTART + ex.Message + ("ERROR3:There was a problem connecting to the Google server" + ERRORSTRINGEND))
Catch ex As System.Security.SecurityException
Return (ERRORSTRINGSTART + ("ERROR4:You do not have permission to make HTTP connections. Please check your assembly's permission s" & _
"ettings" + ERRORSTRINGEND))
Catch ex As Exception
Return (ERRORSTRINGSTART + ("ERROR5:An unspecified error occured: " _
+ (ex.Message + ERRORSTRINGEND)))
End Try
End Function
End Class
|
|
|
|
|
I also ran into the protocol violation error. In addition one of the parameter names changed. I changed the following line (urltext -> trtext):
postsourcedata = "lp=" + translationmode + "&tt=urltext&intl=1&doit=done&trtext=" + HttpUtility.UrlEncode(sourcedata);
Thank you for the code Matthew
Steve MunLeeuw
|
|
|
|
|
|
<string xmlns="http://www.redgreenyellowbluegreenpinkpurplewhite.com/Translation/">There was a problem connecting to the Babelfish server
mark
|
|
|
|
|
error message:
There was a problem connecting to the Babelfish server
Mark
|
|
|
|
|
I'm guessing Babelfish might have changed something on their end but this is the error thrown by the original code:
An unspecified error occured: System.Net.WebException: The underlying connection was closed: The server committed an HTTP protocol violation. at System.Net.HttpWebRequest.CheckFinalStatus() at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.HttpWebRequest.GetResponse() at Translation.Translate.BabelFish(String translationmode, String sourcedata) in c:\documents and settings\xxxx\translation\translate.asmx.cs:line 96
This is the line 96 in question:
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
Anyone know what is wrong?
|
|
|
|
|
This is my VB.net version but it has an error and I can't fix it. Maybe someone can help me with it.
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.IO
Imports System.Net
Imports System.ComponentModel
<WebService(Namespace:="http://www.redgreenyellowbluegreenpinkpurplewhite.com/Translation/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
Public Class WebService
Inherits System.Web.Services.WebService
ReadOnly VALIDTRANSLATIONMODES As String() = New String() {"zh_en", "zt_en", "en_zh", "en_zt", "en_nl", "en_fr", "en_de", "en_el", "en_it", "en_ja", "en_ko", "en_pt", "en_ru", "en_es", "nl_en", "nl_fr", "fr_en", "fr_de", "fr_el", "fr_it", "fr_pt", "fr_nl", "fr_es", "de_en", "de_fr", "el_en", "el_fr", "it_en", "it_fr", "ja_en", "ko_en", "pt_en", "pt_fr", "ru_en", "es_en", "es_fr"}
Const BABELFISHURL As String = "http://babelfish.altavista.com/babelfish/tr"
Const BABELFISHREFERER As String = "http://babelfish.altavista.com/"
Const ERRORSTRINGSTART As String = "<font color=red>"
Const ERRORSTRINGEND As String = "</font>"
Public Sub New()
InitializeComponent()
End Sub
Private components As IContainer = Nothing
Private Sub InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing AndAlso Not (components Is Nothing) Then
components.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
<WebMethod()> _
Public Function BabelFish(ByVal translationmode As String, ByVal sourcedata As String) As String
Try
If translationmode Is Nothing OrElse translationmode.Length = 0 Then
Throw New ArgumentNullException("translationmode")
End If
If sourcedata Is Nothing OrElse translationmode.Length = 0 Then
Throw New ArgumentNullException("sourcedata")
End If
translationmode = translationmode.Trim
sourcedata = sourcedata.Trim
Dim validtranslationmode As Boolean = False
Dim i As Integer = 0
While i < VALIDTRANSLATIONMODES.Length
If VALIDTRANSLATIONMODES(i) = translationmode Then
validtranslationmode = True
' break
End If
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
End While
If Not validtranslationmode Then
Return ERRORSTRINGSTART + "The translationmode specified was not a valid translation translationmode" + ERRORSTRINGEND
End If
Dim uri As Uri = New Uri(BABELFISHURL)
Dim request As HttpWebRequest = CType(WebRequest.Create(uri), HttpWebRequest)
request.Referer = BABELFISHREFERER
Dim postsourcedata As String
postsourcedata = "lp=" + translationmode + "&tt=urltext&intl=1&doit=done&urltext=" + HttpUtility.UrlEncode(sourcedata)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = postsourcedata.Length
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
Dim writeStream As Stream = request.GetRequestStream
Dim encoding As UTF8Encoding = New UTF8Encoding
Dim bytes As Byte() = encoding.GetBytes(postsourcedata)
writeStream.Write(bytes, 0, bytes.Length)
writeStream.Close()
Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
Dim responseStream As Stream = response.GetResponseStream
Dim readStream As StreamReader = New StreamReader(responseStream, System.Text.Encoding.UTF8)
Dim page As String = readStream.ReadToEnd
Dim reg As Regex = New Regex("<div style=padding:10px;>((?:.|\n)*?)</div>", RegexOptions.IgnoreCase)
Dim matches As MatchCollection = reg.Matches(page)
If Not (matches.Count = 1) OrElse Not (matches(0).Groups.Count = 2) Then
Return ERRORSTRINGSTART + "The HTML returned from Babelfish appears to have changed. Please check for an updated regular expression" + ERRORSTRINGEND
End If
Return matches(0).Groups(1).Value
Catch ex As ArgumentNullException
Return ERRORSTRINGSTART + ex.Message + ERRORSTRINGEND
Catch ex As ArgumentException
Return ERRORSTRINGSTART + ex.Message + ERRORSTRINGEND
Catch generatedExceptionVariable0 As WebException
Return generatedExceptionVariable0.ToString
'Return ERRORSTRINGSTART + "There was a problem connecting to the Babelfish server" + ERRORSTRINGEND
Catch generatedExceptionVariable1 As System.Security.SecurityException
Return ERRORSTRINGSTART + "Error: you do not have permission to make HTTP connections. Please check your assembly's permission settings" + ERRORSTRINGEND
Catch ex As Exception
Return ERRORSTRINGSTART + "An unspecified error occured: " + ex.Message + ERRORSTRINGEND
End Try
End Function
End Class
If you run that you will get an error on this line - Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
The error is - System.Net.WebException: The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF at System.Net.HttpWebRequest.GetResponse()
at WebService.BabelFish(String translationmode, String sourcedata) in C:\Inetpub\wwwroot\translate\App_Code\WebService.vb:line 71
So help me if you can.
Thanks
Levy
|
|
|
|
|
Simply paste this line of code over the existing declaration in the code sample.
readonly string[] VALIDTRANSLATIONMODES = new string[] {"zh_en", "zt_en", "en_zh", "en_zt", "en_nl", "en_fr", "en_de", "en_el", "en_it", "en_ja", "en_ko", "en_pt", "en_ru", "en_es", "nl_en", "nl_fr", "fr_en", "fr_de", "fr_el", "fr_it", "fr_pt", "fr_nl", "fr_es", "de_en", "de_fr", "el_en", "el_fr", "it_en", "it_fr", "ja_en", "ko_en", "pt_en", "pt_fr", "ru_en", "es_en", "es_fr"};
|
|
|
|
|
I received the following error while trying to use this service.
"The underlying connection was closed: The server committed an HTTP protocol violation."
I found a solution to the problem at:
http://dotnetjunkies.com/WebLog/afontes/archive/2004/10/15/28726.aspx?Pending=true
I needed to add the following to the web.config file.
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
FYI: My dotNET version is 1.1.4322.2032.
Hope this helps,
Richard Teel
|
|
|
|
|
According to this weblog this is a workaround for what is essentially an invalid HTTP header that is now properly rejected:
http://weblogs.asp.net/gzunino/archive/2004/09/05/225881.aspx
This workaround will work for .NET Framework v1.1 SP1, but it will crash a system that only has the original .NET Framework. This adds complexity during installation (to patch or not to patch)?
I've used Ethereal to look at the actual request and could not see which is the invalid header, or what is missing.
Any suggestions?
|
|
|
|
|
Excuse me..
My Englsih is not excellent.I am korean and beginer for programing ^^
What i want to ask is this....
First of all, i made an application by "WebForm" based on C#~~~ and i put on "UserControl"on the Webform.
When i connected to the service at the explore of the other computer, i couldn't see the "UserControl" .....
Do you know how to putout the "UserControl" ?
Although it's hard to understand my saying, could you give me some advice?
Have a good time bye...
|
|
|
|
|
Hi there,
Reached your site via peldi.com. Any ideas on how I'd go about modifying BabelFish for Flash chat app?
Thanks.
Jason
|
|
|
|
|
|