This Header function is from VB. I am trying to do the same with C# using HttpWebRequest and having trouble. Can anyone help to show me what i am doing wrong?

Here is a VB function which is sent using winsock.

Function Header(Question As String, Answer As String) As String
Dim str As String
str = "question=" & CaptChaQuestion & "&.intl=us&answer=" & CaptChaAnswer
Header = "POST /captcha1 HTTP/1.1" & vbCrLf & _
"Accept: */*" & vbCrLf & _
"Referer: Project You Are Working On" & vbCrLf & _
"Accept-Language: en-us" & vbCrLf & _
"Content-Type: application/x-www-form-urlencoded" & vbCrLf & _
"Accept-Encoding: gzip, deflate" & vbCrLf & _
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)" & vbCrLf & _
"Host: captcha.chat.yahoo.com" & vbCrLf & _
"Content-Length: " & Len(str) & vbCrLf & _
"Connection: Keep-Alive" & vbCrLf & _
"Cache-Control: no-cache" & vbCrLf & _
"Cookie: " & vbCrLf & vbCrLf & str

End Function

Here is what i have been working on in C# but cant seem to get it just right.

public static string HttpPost(string URI, string Parameters)
{
// Create a 'HttpWebRequest' object.
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI);

CookieContainer cookieJar = new CookieContainer();
req.CookieContainer = cookieJar;
req.Referer = "http://captcha.chat.yahoo.com/go/captchat/";
req.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6";
req.KeepAlive = true;
req.Referer = URI;
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";


byte[] bytes = System.Text.Encoding.UTF8.GetBytes(Parameters);
req.ContentLength = bytes.Length;

Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
os.Close();

WebResponse resp = req.GetResponse();
if (resp == null) return null;

StreamReader sr = new StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}

Note: Parameters in my method is the same as str = "question=" & CaptChaQuestion & "&.intl=us&answer=" & CaptChaAnswer in VB

Anybody? I assure you this is not for anything malicious or illegal. Please, i really need help with this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.