I Want to solve captcha using 2captcha API.
string Key = "12345678901234567890123456789012";
string Method = "base64";
string DataForPost = $"key={Key}&method={Method}&imginstructions={Imginstructions}";
request.ContentLength = DataForPost.Length;
string responseText = "";
byte[] bytes = Encoding.ASCII.GetBytes(DataForPost);
request.ContentLength = bytes.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Close();
}
using (WebResponse resp = request.GetResponse())
{
Stream respStream = resp.GetResponseStream();
using (StreamReader sr = new StreamReader(respStream))
{
responseText = sr.ReadToEnd();
}
}
Imginstruction is base64 string for image and size is about 4KB. When I ran above code, I got error message - "ERROR_ZERO_CAPTCHA_FILESIZE". This error means 'File size under 100bytes', but I cannot understand because file size is about 4kb.
If you have any idea for this problem, please tell to me.
Related
I'm new to c# language. I want to write this code to post data to web url:
byte[] data = Encoding.ASCII.GetBytes($"Identifier={"anyUsername"}&Password={"Password"}");
WebRequest request = WebRequest.Create("http://users.tclnet.ir/Authentication/LogOn");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
string responseContent = null;
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
using (StreamReader sr99 = new StreamReader(stream))
{
responseContent = sr99.ReadToEnd();
}
}
}
but i want get cookies from that answer,how can i write code to achieve it?
I can't seem to figure out why I keep getting the following error:
Bytes to be written to the stream exceed the Content-Length bytes size specified.
at the following line:
writeStream.Write(bytes, 0, bytes.Length);
This is on a Windows Forms project. If anyone knows what is going on here I would surely owe you one.
private void Post()
{
HttpWebRequest request = null;
Uri uri = new Uri("xxxxx");
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
request.ContentLength = doc.InnerXml.Length;
using (Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(doc.InnerXml);
writeStream.Write(bytes, 0, bytes.Length);
}
string result = string.Empty;
request.ProtocolVersion = System.Net.HttpVersion.Version11;
request.KeepAlive = false;
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8))
{
result = readStream.ReadToEnd();
}
}
}
}
catch (Exception exp)
{
// MessageBox.Show(exp.Message);
}
}
There are three possible options
Fix the ContentLength as described in the answer from #rene
Don't set the ContentLength, the HttpWebRequest is buffering the data, and sets the ContentLength automatically
Set the SendChunked property to true, and don't set the ContentLength. The request is send chunk encoded to the webserver. (needs HTTP 1.1 and has to be supported by the webserver)
Code:
...
request.SendChunked = true;
using (Stream writeStream = request.GetRequestStream())
{ ... }
The Encoded byte array from your InnerXml might be longer as some characters in an UTF8 encoding take up 2 or 3 bytes for a single character.
Change your code as follows:
using (Stream writeStream = request.GetRequestStream())
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] bytes = encoding.GetBytes(doc.InnerXml);
request.ContentLength = bytes.Length;
writeStream.Write(bytes, 0, bytes.Length);
}
To show exactly what is going on, try this in LINQPad:
var s = "é";
s.Length.Dump("string length");
Encoding.UTF8.GetBytes(s).Length.Dump("array length");
This will output:
string length: 1
array length: 2
and now use an e without the apostrophe:
var s = "e";
s.Length.Dump("string length");
Encoding.UTF8.GetBytes(s).Length.Dump("array length");
which will output:
string length: 1
array length: 1
So remember: string length and the number of bytes needed for a specific encoding might differ.
I'm trying to POST to a website where the only parameter needed is "description." I believe it has something to do with the way that I am encoding my data. Am I missing something/going about this the wrong way?
string postData = String.Format("description=" + description + "&");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Method = WebRequestMethods.Http.Post;
wr.ContentLength = byteArray.Length;
wr.ContentType = "application/x-www-form-urlencoded";
Stream postStream = wr.GetRequestStream();
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
WebResponse response = await wr.GetResponseAsync();
HttpWebResponse webResponse = (HttpWebResponse)response;
using (var stm = response.GetResponseStream())
{
using (var reader = new StreamReader(stm))
{
var content = await reader.ReadToEndAsync();
reader.Close();
stm.Close();
return content;
}
}
The stream you are trying to read apparently doesn't support seeking. You can also verify that programmatically through its CanSeek property.
If you want the same data in a stream that you can seek, consider reading the entirety of your current stream to a byte[] buffer, then constructing a MemoryStream out of that buffer for use with your StreamReader.
I want to fetch a web page to analyze stock information. I use the following sample code to get html data using c#. While it compiles, running it always ends in an error.
The following is my sample code:
string urlAddress = "http://pchome.syspower.com.tw/stock/sto0/ock2/sid2404.html";
var request = (HttpWebRequest)WebRequest.Create(urlAddress);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = 1414;
var requestStream = request.GetRequestStream();
requestStream.Write(Encoding.UTF8.GetBytes("is_check=1"), 0, 10);
requestStream.Close();
var response = (HttpWebResponse)request.GetResponse();
var sr = new StreamReader(response.GetResponseStream());
string rawData = sr.ReadToEnd();
sr.Close();
response.Close();
Does anyone know how to solve this issue?
The errors were mostly related to the order of your code and the closing of Stream objects your were still going to use.
Also I recommend to use using where possible to correctly dispose objects.
Use this code:
string rawData;
byte[] bytes = Encoding.UTF8.GetBytes("is_check=1");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
rawData = sr.ReadToEnd();
}
}
}
So I have a function like so:
private String SendRequest(String jsonRequest)
{
WebRequest webRequest = WebRequest.Create(_url);
byte[] paramBytes = Encoding.UTF8.GetBytes(jsonRequest);
byte[] responseBytes;
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.ContentLength = paramBytes.Length;
webRequest.Headers.Add("X-Transmission-Session-Id", _sessionId);
using (Stream oStream = webRequest.GetRequestStream())
{
oStream.Write(paramBytes, 0, paramBytes.Length);
}
WebResponse webResponse = webRequest.GetResponse();
using (Stream iStream = webResponse.GetResponseStream())
{
responseBytes = new byte[webResponse.ContentLength];
iStream.Read(responseBytes, 0, (int) webResponse.ContentLength);
}
return Encoding.UTF8.GetString(responseBytes);
}
The problem is, at the iStream.Read() stage, some of the bytes are lost. Using wireshark reveals all the bytes are sent to this machine, however .Net is loosing them somewhere along the way. In my current debugging session, for example, where webResponse.ContentLength = 4746 byte[3949] to byte[4745] are all 0's, but they should be populated. As a result, the UTF8 JSON string cuts off early and I can't deserialise my JSON.
I thought the code was pretty clear cut, I can't see where it's going wrong to loose those bytes.
Thanks for any help!
When reading from the stream you can get less bytes than requested.
http://msdn.microsoft.com/en-us/library/system.io.stream.read.aspx
The total number of bytes read into the buffer. This can be less than
the number of bytes requested if that many bytes are not currently
available, or zero (0) if the end of the stream has been reached.
msdn example for WebResponse.GetResponseStream():
http://msdn.microsoft.com/en-us/library/system.net.webresponse.getresponsestream.aspx
I fixed it by using StreamReader instead :)
private String SendRequest(String jsonRequest)
{
WebRequest webRequest = WebRequest.Create(_url);
byte[] paramBytes = Encoding.UTF8.GetBytes(jsonRequest);
String jsonResponse;
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.ContentLength = paramBytes.Length;
webRequest.Headers.Add("X-Transmission-Session-Id", _sessionId);
using (Stream oStream = webRequest.GetRequestStream())
{
oStream.Write(paramBytes, 0, paramBytes.Length);
oStream.Close();
}
WebResponse webResponse = webRequest.GetResponse();
using (Stream iStream = webResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(iStream, Encoding.UTF8);
jsonResponse = reader.ReadToEnd();
reader.Close();
iStream.Close();
}
return jsonResponse;
}