I can't create thread on PHPBB3 forum - c#

My problem is that I get 0 bytes response from server (I'm using Fiddler2 to check this out).
Picture:
What I'm doing:
Log in. I get cookies.
Creating thread
public void CreateTopic(string subject, string message, string forumId)
{
HttpWebResponse response = null;
string source = string.Empty;
string lastClick= string.Empty;
string creationTime= string.Empty;
string formToken = string.Empty;
// GET
while(true)
{
webRequest =
(HttpWebRequest) HttpWebRequest.Create(Url + "posting.php?mode=post&f=" + forumId + "&sid=" + sid);
webRequest.KeepAlive = true;
webRequest.Method = "GET";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Host = Url.Replace("http://", "").Replace("/", "");
webRequest.CookieContainer = cookieJar;
ServicePointManager.Expect100Continue = false;
try
{
response = (HttpWebResponse) webRequest.GetResponse();
UpdateSid(Url + "posting.php?mode=post&f=" + forumId + "&sid=" + sid);
}
catch (Exception ex)
{
continue;
}
break;
}
StreamReader streamReader = new StreamReader(response.GetResponseStream());
source = streamReader.ReadToEnd();
streamReader.Close();
response.Close();
// Get stuff
// last click
Match lastClickMatch = Regex.Match(source, "name=\"lastclick\" value=\"([0-9]{10})\" />");
if (lastClickMatch.Success) lastClick = lastClickMatch.Groups[1].Value;
// creation time
Match creationTimeMatch = Regex.Match(source, "name=\"creation_time\" value=\"([0-9]{10})\" />");
if (creationTimeMatch.Success) creationTime = creationTimeMatch.Groups[1].Value;
// form token
Match formTokenMatch = Regex.Match(source, "name=\"form_token\" value=\"(.{40})\" />");
if (formTokenMatch.Success) formToken = formTokenMatch.Groups[1].Value;
// POST
webRequest = (HttpWebRequest)WebRequest.Create(Url + "posting.php?mode=post&f=" + forumId + "&sid=" + sid);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Host = Url.Replace("http://", "").Replace("/", "");
webRequest.CookieContainer = cookieJar;
string data = "icon=&subject=" + Encode(subject) + "&addbbcode20=100&message=" + Encode(message) + "&attach_sig=on&post=Submit&lastclick=" + lastClick + "&creation_time=" + creationTime + "&form_token=" + formToken;
byte[] byte1 = Encoding.UTF8.GetBytes(data);
webRequest.ContentLength = byte1.Length;
ServicePointManager.Expect100Continue = false;
Stream stream = webRequest.GetRequestStream();
stream.Write(byte1, 0, byte1.Length);
stream.Close();
response = (HttpWebResponse)webRequest.GetResponse();
UpdateSid(Url + "posting.php?mode=post&f=" + forumId + "&sid=" + sid);
response.Close();
}
I get this on fiddler:
RAW:
POST http://stuff.com/posting.php?mode=post&f=8&sid=6d278be1e705fe0261f253bbc96ab7a4
HTTP/1.1 Content-Type: application/x-www-form-urlencoded
Host: stuff.com
Cookie: phpbb3_goeyz_u=55443;phpbb3_goeyz_k=;phpbb3_goeyz_sid=6d278be1e705fe0261f253bbc96ab7a4
Content-Length: 165
icon=&subject=g&addbbcode20=100&message=d&attach_sig=on&post=Submit&lastclick=1323904393&creation_time=1323904393&form_token=b81c23e308f0bd3a0a73907efcc8c18e5dba0f7f
Response RAW:
HTTP/1.1 302 Found
Date: Wed, 14 Dec 2011 23:13:14 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Powered-By: PHP/5.3.2-1ubuntu4.10
Location: http://stuff.com/games.html
Vary: Accept-Encoding
Content-Length: 0
Connection: close
Content-Type: text/html
I have no idea what is going on. If you will help me I will be grateful. I'm sorry for my English.

I found this snippet in the source of phpBB 3.0.9:
203 // Check permissions
204 if ($user->data['is_bot'])
205 {
206 redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
207 }
So they might be pulling you a trick here. Are you sure you are allowed to do what you're trying to?
Anyhow, I think you might to want to change your user agent:
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.36 Safari/535.7";
This might help not being identified as a bot, which perhaps takes place because the default UserAgent value is null.

I found out why it didn't create thread. It checks:
if ($user->data['is_bot'])
If you don't want to be a bot, then you have to WAIT for like 8 seconds before sending POST:
Thread.Sleep(8100);
It resolved all problems for me.

Its returning a status code of 302 Found, which means the server is attempting to redirect you to another page (in your example, "http://stuff.com/games.html").

Related

Sending Attachment to HttpRequest POST C#

I'm trying to send an attachment using HttpRequest, but the server give me a 500 Internal Server Error.
If i make the same call from SOAP UI, it will success with this result.
HTTP/1.1 200 OK Date: Fri, 30 Apr 2021 13:47:00 GMT Server: Apache Strict-Transport-Security: max-age=63072000; includeSubDomains;
preload Keep-Alive: timeout=5, max=100 Connection: Keep-Alive
Transfer-Encoding: chunked Content-Type: multipart/related;
boundary="MIMEBoundary_ccc0c7786f280ccc7fb981fb343b4463e574457582881961";
type="application/xop+xml";
start="0.dcc0c7786f280ccc7fb981fb343b4463e574457582881961#apache.org";
start-info="text/xml"
--MIMEBoundary_ccc0c7786f280ccc7fb981fb343b4463e574457582881961 Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary Content-ID:
0.dcc0c7786f280ccc7fb981fb343b4463e574457582881961#apache.org
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
...</soapenv:Envelope>
--MIMEBoundary_ccc0c7786f280ccc7fb981fb343b4463e574457582881961--
So i'm mistaking the link between soap request and attachment.
What could be the error inside the code?
The code is called by a c# Web Api and is calling a SOAP POST resource.
public static string Start()
{
var _url = "...";
var _action = "...";
XmlDocument soapEnvelopeXml = SoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
}
return "";
}
private static XmlDocument SoapEnvelope()
{
XmlDocument soapEnvelopeDocument = new XmlDocument();
soapEnvelopeDocument.LoadXml(
#"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/""
....
</soapenv:Envelope>");
return soapEnvelopeDocument;
}
public static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
var data = GetFromData("dummy.pdf", #"C:\dummy.pdf", webRequest);
stream.Write(data, 0, data.Length);
soapEnvelopeXml.Save(stream);
}
}
public static byte[] GetFromData(string fileName, string filePath, HttpWebRequest request)
{
var attachment = System.IO.File.ReadAllBytes(filePath);
string boundaryString = Guid.NewGuid().ToString();
MemoryStream postDataStream = new MemoryStream();
StreamWriter postDataWriter = new StreamWriter(postDataStream);
request.ContentType = "multipart/form-data; boundary=" + boundaryString;
postDataWriter.Write("\r\n--" + boundaryString + "\r\n");
postDataWriter.Flush();
postDataWriter.Write("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", "file", fileName, MimeMapping.GetMimeMapping(fileName));
postDataWriter.Flush();
postDataStream.Write(attachment, 0, attachment.Length);
postDataWriter.Write("\r\n--" + boundaryString + "--\r\n");
postDataWriter.Flush();
return postDataStream.ToArray();
}
Thank you

Request to Yandex Speech

I try to set request to recognize wav file->to text with Yandex Speech API via desktop app.
Example of request from documentation:
POST /asr_xml?uuid=01ae13cb744628b58fb536d496daa1e6&key=developers-simple- key&topic=maps HTTP/1.1
Content-Type: audio/x-speex
User-Agent: Dalvik/1.2.0 (Linux; U; Android 2.2.2; LG-P990 Build/FRG83G)
Host: asr.yandex.net
Transfer-Encoding: chunked
7d0
...
chunked body
So, i register at developer forum, get api key and write simple code:
public string RecognizeSpeech(byte[] bytes, String uuid, String apiKey, string topic = "queries", string lang = "ru-RU")
{
try
{
var uri = string.Format("https://asr.yandex.net/asr_xml?" +
"uuid={0}" +
"&key={1}" +
"&topic={2}" +
"&lang={3}", uuid, apiKey, topic, lang);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "audio/x-wav";//"audio/x-pcm;bit=16;rate=16000";
request.ContentLength = bytes.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);// exception here
}
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
return reader.ReadToEnd();
}
catch (Exception ex)
{
...
}
return "";
}
I try to use audio/x-wav or audio/x-pcm;bit=16;rate=16000, but I get error:
Unable to write data to the transport connection: remote host forcibly ripped existing connection.
(use google translate)
byte[] bytes- is:
var audioBytes = File.ReadAllBytes(#"file.wav");
P.S. documentation on Russian language
This gist set POST request to Yandex Speech Cloud.
You need to make valid audio file (i use Freemake) - pcm,16 bit, 16000 hz, mono channel.
Here the solution:
public string PostMethod(byte[] bytes)
{
string postUrl = "https://asr.yandex.net/asr_xml?" +
"uuid=01ae13cb744628b58fb536d496daa1e6&" +
"key="+your_api_key_here+"&" +
"topic=queries";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(postUrl);
request.Method = "POST";
request.Host = "asr.yandex.net";
request.SendChunked = true;
request.UserAgent = "Oleg";
request.ContentType = "audio/x-pcm;bit=16;rate=16000";
request.ContentLength = bytes.Length;
using (var newStream = request.GetRequestStream())
{
newStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseToString = "";
if (response != null)
{
var strreader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
responseToString = strreader.ReadToEnd();
}
int index = responseToString.IndexOf("<variant confidence=\"1\">");
responseToString = responseToString.Substring(index + 24, responseToString.Length - index - 24);
int index2 = responseToString.IndexOf("</variant>");
responseToString = responseToString.Substring(0, index2);
return responseToString;
}

C# Upload file error - Corrupt form data: no leading boundary

I am using this code below to authenticate and upload file to a website, but I am getting an error "Corrupt form data: no leading boundary".
In the Fiddler I see this, ( I see the exact same thing when I upload the file using browser), not sure why I am getting an error when I am doing using C# HttpWebRequest/WebClient
Fiddler Info:
------WebKitFormBoundary9ewWOMyBmk0YAhTL
Content-Disposition: form-data; name="FileSubmitted"; filename="SAMPLE_0001.xls"
Content-Type: application/vnd.ms-excel
------WebKitFormBoundary9ewWOMyBmk0YAhTL
Content-Disposition: form-data; name="FileSubmittedValue"
C:\path\SAMPLE_0001.xls
------WebKitFormBoundary9ewWOMyBmk0YAhTL--
Assumptions:
I hard coded the 'boundary' value, because I am not sure how to get the 'boundary' value after login.
Code: Copied from Stackoverflow
static void Main()
{
NameValueCollection nvCollection = new NameValueCollection();
CookieAwareWebClient webClient = new CookieAwareWebClient();
nvCollection.Clear();
nvCollection["Name"] = "ABC";
nvCollection["Password"] = "XYZ";
//Login to the Site
byte[] responseBytes = webClient.UploadValues("https://www.somesite.com/login.cfm", "POST", nvCollection);
string resultAuthTicket = Encoding.UTF8.GetString(responseBytes);
//Get Cookies
CookieCollection cookies = webClient.CookieContainer.GetCookies(new Uri("https://www.somesite.com/login.cfm"));
//
string URL = "https://www.somesite.com/app/request.cfm";
string boundary = "----WebKitFormBoundary9ewWOMyBmk0YAhTL";
string FilePath = "C:\\Users\\user.name\\Desktop\\SAMPLE_0001.xls";
byte[] fileData = GetMultipartFormData(new Dictionary<string, object>() { { "FileSubmitted", FilePath } }, boundary);
PostForm(URL, "", "", fileData, boundary, cookies);
}
private static byte[] GetMultipartFormData(Dictionary<string, object> postParameters, string boundary)
{
Encoding encoding = Encoding.UTF8;
Stream formDataStream = new System.IO.MemoryStream();
bool needsCLRF = false;
foreach (var param in postParameters)
{
// Thanks to feedback from commenters, add a CRLF to allow multiple parameters to be added.
// Skip it on the first parameter, add it to subsequent parameters.
if (needsCLRF)
formDataStream.Write(encoding.GetBytes("\r\n"), 0, encoding.GetByteCount("\r\n"));
needsCLRF = true;
{
string postData = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n",
boundary,
param.Key,
param.Value,
"application/vnd.ms-excel");
formDataStream.Write(encoding.GetBytes(postData), 0, encoding.GetByteCount(postData));
string postData2 = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"{1}\" \r\n\r\n{2}",
boundary,
"FileSubmittedValue",
"C:\\path\\SAMPLE_0001.xls");
formDataStream.Write(encoding.GetBytes(postData2), 0, encoding.GetByteCount(postData2));
}
}
// Add the end of the request. Start with a newline
string footer = "\r\n--" + boundary + "--\r\n";
formDataStream.Write(encoding.GetBytes(footer), 0, encoding.GetByteCount(footer));
// Dump the Stream into a byte[]
formDataStream.Position = 0;
byte[] formData = new byte[formDataStream.Length];
formDataStream.Read(formData, 0, formData.Length);
formDataStream.Close();
return formData;
}
private static HttpWebResponse PostForm(string postUrl, string userAgent, string contentType, byte[] formData, string boundary, CookieCollection cookies)
{
HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
if (request == null)
{
throw new NullReferenceException("request is not a http request");
}
// Set up the request properties.
request.UserAgent = userAgent;
request.ContentLength = formData.Length;
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.Method = "POST";
request.KeepAlive = true;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
request.Host = "secure.somesite.com";
request.Referer = String.Format("https://secure.somesite.com/app/request.cfm?CFID={0}&CFTOKEN={1}", cookies[0].Value, cookies[1].Value);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36";
// Send the form data to the request.
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(formData, 0, formData.Length);
requestStream.Close();
}
try
{
return request.GetResponse() as HttpWebResponse;
}
catch (WebException wex)
{
var pageContent = new StreamReader(wex.Response.GetResponseStream())
.ReadToEnd();
return null;
}
return null;
}
Error:
<head><title>JRun Servlet Error</title></head><h1>500 </h1><body>
<pre>
Corrupt form data: no leading boundary: != ------WebKitFormBoundary9ewWOMyBmk0YAhTL</pre><br><pre>
java.io.IOException: Corrupt form data: no leading boundary: != ------WebKitFormBoundary9ewWOMyBmk0YAhTL
at com.oreilly.servlet.multipart.MultipartParser.<init>(MultipartParser.java:176)
at com.oreilly.servlet.multipart.MultipartParser.<init>(MultipartParser.java:95)
at coldfusion.filter.FormScope.fillMultipart(FormScope.java:170)
at coldfusion.filter.FusionContext.SymTab_initForRequest(FusionContext.java:435)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:33)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.RequestThrottleFilter.invoke(RequestThrottleFilter.java:126)
at coldfusion.CfmServlet.service(CfmServlet.java:175)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:86)
at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:94)
at jrun.servlet.FilterChain.service(FilterChain.java:101)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:203)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)
</pre></body>
Don't Knows: Since I copied this source code from Stackoverflow, I don't know if the file really getting uploaded to C:\Path folder on server (look at the Fiddler data).
Update: When I try to upload file using browser I see Content-Length: 49504, but when I try using C# program Content-Length: 385 (Even when I use the same file in both browser and C# program)
Also, when I try now I am getting "No data was received in the uploaded file SAMPLE_0001.xls"
The code is missing this line of code, which is the actual file content.
formDataStream.Write(fileToUpload.File, 0, fileToUpload.File.Length);
Thanks all for your help!

I cannot get cookie from C# HTTPWebRequest

I have a requirement to get cookies from login because i will apply this cookie in the same page. Here is my code
string boundary = "41184676334";
string username = "username";
string password = "password";
string login_post_data =
"POSTDATA=-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"login\"" +
"\n\n" + username +
"\n-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"key\"" +
"\n\n" + password +
"\n-----------------------------" + boundary +
"\nContent-Disposition: form-data; name=\"clcode\"" +
"\n\n" +
"\n-----------------------------" + boundary + "--";
var cookies = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://site.com/cgi-bin/login.pl?logout");
request.CookieContainer = cookies;
request.Method = "POST";
request.ContentType = "multipart/form-data; boundary=---------------------------" + boundary;
request.Host = "site.com";
request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.Referer = "https://site.com/cgi-bin/login.pl?logout";
//request.Headers.Add("POSTDATA", post_data);
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(StrToByteArray(login_post_data), 0, StrToByteArray(login_post_data).Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string temp = reader.ReadToEnd();
richTextBox1.Text = temp;
}
foreach (string c in response.Cookies)
{
listBox1.Items.Add(c);
}
The thing is "https://site.com/cgi-bin/login.pl?logout" is the same page after i logged in and i need to pass the cookie
It looks like you try to log in and log out at the same time with your request, sent to URL
https://site.com/cgi-bin/login.pl?logout
Remove the ?logout parameter and try again. Please update your question if it doesn't change anything.
Please explain further, what you're trying to achieve, so we can discuss if the code is correct.

How do I login, navigate, and return data from a protected website, so far everything I'm doing isn't working

and while I've found many articles and other information about how to GET and POST using HttpWebRequest and Response, I find myself having a hard time getting things to work like I would expect them to work.
I've been playing with several ideas that I have found but so far, nothing is working... I'll post my code:
private void start_post()
{
string username = txtUser.Text;
string password = txtPassword.Text;
string strResponce;
byte[] buffer = Encoding.ASCII.GetBytes("username="+username+"&password="+password);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(txtLink.Text);
WebReq.Method = "POST";
//WebReq.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
WebReq.Headers.Add("Translate", "F");
WebReq.AllowAutoRedirect = true;
WebReq.CookieContainer = cookieJar;
WebReq.KeepAlive = true;
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
//txtResult.Text = WebResp.StatusCode.ToString() + WebResp.Server.ToString();
Stream answer = WebResp.GetResponseStream();
StreamReader _answer = new StreamReader(answer);
strResponce = _answer.ReadToEnd();
//txtResult.Text = txtResult.Text + _answer.ReadToEnd();
answer.Close();
_answer.Close();
foreach (Cookie cookie in WebResp.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
txtResult.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
}
if (strResponce.Contains("Log On Successful") || strResponce.Contains("already has a webseal session"))
{
MessageBox.Show("Login success");
foreach (Control cont in this.Controls)
{
cont.Visible = true;
}
}
else
{
MessageBox.Show("Login Failed.");
}
}
Here in code, I'm able to get all the way to the bottom, and still get a login failed when I navigate to http://www.comicearth.com (my own site, php and apache) I created a form and from that form, I type in the password and username. When it do this, it says failed, which is ok. I'm also using Fidder to watch to see what all is happening.
So from this, I know I'm doing something wrong from the code below.
However, when I navigate to another web application, I get the follow error on the line:
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
"Content-Length or Chunked Encoding cannot be set for an operation that does not write data."
I'm trying to find what is wrong, and everything I have said that it's because of a 302 redirect...
So, looking in Fiddler I can see a huge difference between when I try to post the data and when I login via a web page. So I know I'm not doing enough, but I don't know where to look.
My goal is to build an application that is able to login to the website, and then via their search option pull out necessary data that currently we have our users do manually, if I can automate some of the tedious work, it would really help everyone out. However, I'm currently stuck on logging in, understanding cookies, etc... Also, the website uses frames, I don't know if that will be an issue, but I figured I'd post that info, just in case it is another hurdle I haven't come across yet.
Let me know if you need me to see more code, currently I'm using the httpwebrequest and httpwebresponse and I've read other informaiton about web client.
I've downloaded and have played around with htmlagilitypack but at this time not to sure I'm 100% good on how that all works as well.
If you know of any good articles, or other information that covers this topic more in depth or have anything that I can try, let me know.
Thanks so much for your time.
Update with new code, see my comment below as well: - Ok, I found out that because of the redirect I was getting that one error message: "Content-Length or Chunked Encoding etc..." and so I turned the allowAutoRedirect = false and now I look for the "location" tag, and redirect myself etc, which got rid of this message, however, I'm still not getting logged into the site, which is disappointing and I can't figure out why at the moment. :S
private void start_post2()
{
string username = txtUser.Text;
string password = txtPassword.Text;
Uri link = new Uri(txtLink.Text);
string postArgs = string.Format(#"userId={0}&password={1}", username, password);
byte[] buffer = Encoding.ASCII.GetBytes(postArgs);
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(txtLink.Text);
WebReq.Method = "POST";
//WebReq.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
//WebReq.ClientCertificates.Add("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
WebReq.AllowAutoRedirect = false;
WebReq.Accept = "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
WebReq.Accept = "*/*";
//WebReq.Headers.Add(HttpRequestHeader.Cookie, cookieJar);
WebReq.CookieContainer = cookieJar;
WebReq.KeepAlive = true;
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
Stream PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
if (WebResp == null) throw new Exception("Response is null");
foreach (Cookie cookie in WebResp.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
//txtResult.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
}
if (!string.IsNullOrEmpty(WebResp.Headers["Location"]))
{
string newLocation = WebResp.Headers["Location"];
//Request the new location
WebReq = (HttpWebRequest)WebRequest.Create(newLocation);
WebReq.Method = "GET";
WebReq.ContentType = "application/x-www-form-unlencoded";
WebReq.AllowAutoRedirect = false;
WebReq.CookieContainer = cookieJar;
WebReq.CookieContainer.Add(WebResp.Cookies);
buffer = Encoding.ASCII.GetBytes("userId=" + username + "&password=" + password);
WebReq.ContentLength = buffer.Length;
PostData = WebReq.GetRequestStream();
PostData.Write(buffer, 0, buffer.Length);
PostData.Close();
WebResp = (HttpWebResponse)WebReq.GetResponse();
foreach (Cookie cookie in WebResp.Cookies)
{
cookieJar.Add(new Cookie(cookie.Name.Trim(), cookie.Value.Trim(), cookie.Path, cookie.Domain));
//txtResult.Text += cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString();
}
}
else if (!string.IsNullOrEmpty(WebResp.Headers["Set-Cookie"]))
{
// thinking...
}
foreach (Cookie cookie in cookieJar.GetCookies(link))
{
MessageBox.Show(cookie.Name.ToString() + Environment.NewLine + cookie.Value.ToString() + Environment.NewLine + cookie.Path.ToString() + Environment.NewLine + cookie.Domain.ToString());
}
StreamReader sr = new StreamReader(WebResp.GetResponseStream());
string responseHtml = sr.ReadToEnd().Trim();
SearchPatient(WebReq, username, password);
}
If its a winform application and the application is just a screen-scraper and not a very big application, U can use Watin for doing the scrapping
here is link for getting started

Categories

Resources