Im trying to submit a specific form programatically, but I allways get the initial page back.
I must be doing something wrong or missing something here.
Im sending the session cookie and some POST data like viewState (that I parse from the initial request), and SessionID (this is the value i change in the form toget data from other years). But in the second request I allways get data for Session 899, instead of the one i request: 875.
Here is the code used:, any help is greatly apreciated
retrieveEdmIndexForSession(875);
protected string retrieveEdmIndexForSession(int sessionId) {
CookieContainer cookies;
HttpWebRequest oRequest;
HttpWebResponse oResponse;
Stream sw;
StreamReader sr;
string pageData;
string PathRemote = #"http://edmi.parliament.uk/EDMi/EDMList.aspx";
/*
* download the index page so we can get Cookies and ViewState from it.
*/
oRequest = (HttpWebRequest)WebRequest.Create(PathRemote);
oRequest.Method = "GET";
oRequest.AllowAutoRedirect = true;
oRequest.CookieContainer = new CookieContainer();
oRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
oRequest.Referer = "http://edmi.parliament.uk/EDMi/EDMList.aspx";
oResponse =(HttpWebResponse) oRequest.GetResponse();
sr = new StreamReader(oResponse.GetResponseStream());
pageData = sr.ReadToEnd();
/*
* extract view state from pageData.
*/
string viewState = this.ExtractViewState(pageData);
/*
* lets submit the form with the parameters we want
*/
oRequest = (HttpWebRequest)WebRequest.Create(PathRemote);
oRequest.Method = "POST";
oRequest.AllowAutoRedirect = true;
oRequest.ContentType = "application/x-www-form-urlencoded";
oRequest.CookieContainer = new CookieContainer();
oRequest.CookieContainer.Add(oResponse.Cookies);
oRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
oRequest.Referer = "http://edmi.parliament.uk/EDMi/EDMList.aspx";
string postdata = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=" + viewState + "&_MenuCtrl%3AddlSession=" + sessionId + "&_MenuCtrl%3A_GoTo.x=57&_MenuCtrl%3A_GoTo.y=14&ddlStatus=1&ddlSortedBy=1";
byte[] buffer = Encoding.UTF8.GetBytes(postdata);
oRequest.ContentLength = buffer.Length;
/*
* Send post data into request stream first
*/
sw = oRequest.GetRequestStream();
sw.Write(buffer, 0, buffer.Length);
sw.Flush();
sw.Close();
/*
* Connect, send and get response
*/
oResponse = (HttpWebResponse)oRequest.GetResponse();
sr = new StreamReader(oResponse.GetResponseStream());
//OnLogUpdated(1, "\r\nStatus Code: " + oResponse.StatusCode);
//OnLogUpdated(1, "\r\nServer: " + oResponse.Server);
pageData = sr.ReadToEnd();
string result = getSessionId(pageData);
//OnLogUpdated(1, "\r\nRestuls: [" + result + "]");
//OnLogUpdated(1, "\r\nPage: [" + pageData + "]");
return pageData;
}
private string ExtractViewState(string str)
{
string viewState = "";
string pattern = "(?<=__VIEWSTATE\" value=\")(?<val>.*?)(?=\")";
Match match = Regex.Match(str, pattern);
if (match.Success)
{
viewState = match.Groups["val"].Value;
viewState = HttpUtility.UrlEncodeUnicode(viewState);
}
return viewState;
}
protected string getSessionId(string str)
{
string sessionId = string.Empty;
str = str.Trim();
string pattern = #"&SESSION=([^']+)'";
Match match = Regex.Match(str, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
sessionId = match.Groups[1].ToString(); ;
}
return sessionId;
}
This is the RAW Request being sent by the .NET script.
POST /EDMi/EDMList.aspx HTTP/1.1
Content-Type:
application/x-www-form-urlencoded
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Referer:
http://edmi.parliament.uk/EDMi/EDMList.aspx
User-Agent: .NET Framework Client
Host: edmi.parliament.uk Cookie:
ASP.NET_SessionId=k55fqarvx2oszp2wxhtrol45
Content-Length: 2431 Expect:
100-continue
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwxMDgyNzIxNDQ2O3Q8O2w8aTwzPjs%2bO2w8dDw7bDxpPDE%2bO2k8Mz47aTw1PjtpPDExPjs%2bO2w8dDw7bDxpPDEzPjtpPDE3Pjs%2bO2w8dDx0PHA8cDxsPERhdGFWYWx1ZUZpZWxkO0RhdGFUZXh0RmllbGQ7PjtsPFNFU1NJT05JRDtJVEVNX1ZBTFVFOz4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bPBNzNuwxs7sYLdUE2omkjw%3d%3d&_MenuCtrl%3AddlSession=875&_MenuCtrl%3A_GoTo.x=57&_MenuCtrl%3A_GoTo.y=14&ddlStatus=1&ddlSortedBy=1
This is the RAW request sent by IE:
POST /EDMi/EDMList.aspx HTTP/1.1
Accept: image/gif, image/jpeg,
image/pjpeg, image/pjpeg,
application/x-shockwave-flash,
application/xaml+xml,
application/vnd.ms-xpsdocument,
application/x-ms-xbap,
application/x-ms-application,
application/vnd.ms-excel,
application/vnd.ms-powerpoint,
application/msword, / Referer:
http://edmi.parliament.uk/EDMi/EDMList.aspx
Accept-Language: en-gb User-Agent:
Mozilla/4.0 (compatible; MSIE 8.0;
Windows NT 5.1; Trident/4.0; .NET CLR
1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; InfoPath.1; .NET CLR 3.0.04506.648; OfficeLiveConnector.1.3;
OfficeLivePatch.0.0; .NET CLR
3.0.4506.2152; .NET CLR 3.5.30729) Content-Type:
application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate Host:
edmi.parliament.uk Content-Length:
2431 Connection: Keep-Alive Pragma:
no-cache Cookie:
WT_FPC=id=83.217.99.254-2364242496.30021299:lv=1249572414567:ss=1249572414567;
ASP.NET_SessionId=vwxgo4rlex1j5m55l0bivrqo
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwxMDgyNzIxNDQ2O3Q8O2w8aTwzPjs%2BO2w8dDw7bDxpPDE%2BO2k8Mz47aTw1PjtpPDExPjs%2BO2w8dDw7bDxpPDEzPjtpPDE3Pjs%2BO2w8dDx0PHA8cDxsPERhdGFWYWx1ZUZpZWxkO0RhdGFUZXh0RmllbGQ7PjtsPFNFU1NJT05JRDtJVEVNX1ZBTFVFOz4%2BOz47dDxpPDIwPjtAPDA4LTA5OzA3LTA4OzA2LTA3OzA1LTA2OzA0LTA1OzAzLTA0OzAyLTAzOzAxLTAyOzAwLTAxOzk5LTAwOzk4LTk5Ozk3LTk4Ozk2L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cookie(WT_FPC=id=83.217.99.254-2364242496.30021299:lv=1249572414567:ss=1249572414567;) witch appers to track visitors using cookies via the WebTrends Cookie Plug-In. Both POST Requests Return HTTP Status Code 302 and redirect to a GET request that returns status 200.
Any ideas ?
You are creating a new CookieContainer each time. Try using the CookieContainer of the index page request as the container for the form submit, it should contain the extra cookie. Alternatively, something other than the form could be passing the extra cookie. Check when the page loads using IEinspector's HttpAnalyzer or Firefox's Firebug to make sure other form elements like images aren't passing the cookie.
I have cracked it. It seems that the .NET server does not like me changing the Status and the Session at the same time.
It works if i change the "ddlStatus" from:
string postdata =
"__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE="
+ viewState + "&_MenuCtrl%3AddlSession=" + sessionId
+ "&_MenuCtrl%3A_GoTo.x=57&_MenuCtrl%3A_GoTo.y=14&ddlStatus=1&ddlSortedBy=1";
to this:
string postdata =
"__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE="
+ viewState + "&_MenuCtrl%3AddlSession=" + sessionId
+ "&_MenuCtrl%3A_GoTo.x=57&_MenuCtrl%3A_GoTo.y=14&ddlStatus=0&ddlSortedBy=1";
I will have to change the ddlStatus in a second POST request, and so on for each change in the form. .NET seems to be very well educated, It can only take one sweet at a time.
Thanks for all the help :)
Maybe this is not related to your problem, but sometimes when you scrape a site the remote server recognize that the one who's browsing is a script and block some operations. I usually workaround this via setting UserAgent property to the request. It's a poor security measure, but often used in my experience.
Here you can find some sample user agent strings.
The only thing that struck me as odd in that request is the Expect header. Apparently it's caused problems for people in the past. To stop that you can use the following line:
System.Net.ServicePointManager.Expect100Continue = false;
If that is the problem it's probably worth setting that property back to whatever it was before, since it's a global variable and may well affect other parts of your code.
Related
I am using C# to make a request to my webserver like this so I can save the data from the client into my model.
This is the route:
Route::get('client/register', 'ClientController#store');
C# request:
string response = Network.sendWebRequest("http://localhost/myproject/public/client/register?identity=" + getIdentity());
As you can see I send a GET request with identity and a base64 encoded string as value.
Then in my store function I decode the base64 encoded string, explode it and try to save it in my database (HINT: The database table is empty, so don't worry the record does not exist already)
ClientController
public function store(Request $request)
{
echo "store function";
$clientIdentityB64 = request('identity');
if (empty($clientIdentityB64)) { return "empty"; }
$clientIdentity = base64_decode($clientIdentityB64);
$identityArr = explode(";", $clientIdentity);
$clientQuery = Client::select("id")
->where("base64Id", "=", $clientIdentityB64)
->get()
->first();
if (is_null($clientQuery)) {
$client = new Client();
$client->ip = $request->ip();
$client->base64Id = $clientIdentityB64;
$client->userName = $identityArr[0];
$client->userDomainName = $identityArr[1];
$client->machineName = $identityArr[2];
$client->osVersion = $identityArr[3];
$client->userAgent = $request->userAgent();
$client->save();
}
}
However, It does not save the model into the database if I make the request. You can see that I output store function at the top of the store function, but I only get the response if I remove $client->save() from the code.
If $client->save() is in the code then I get this response:
Exception: System.Net.WebException: Request canceled: disconnected
bei System.Net.HttpWebRequest.GetResponse()
bei Helper.Network.sendWebRequest(String URL) in C:\Users....
I already echoed out all the values with success, only the save function does not work even though the values are there.
However, It does work If I call the following URL from a normal webbrowser: http://localhost/myproject/public/client/register?identity=somebase64encodedhash.
How to solve this?
This is my network class which I used above:
public static class Network
{
public static string sendWebRequest(string URL)
{
System.Net.WebRequest request = System.Net.WebRequest.Create(URL);
try {
System.Net.WebResponse response = request.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(response.GetResponseStream());
return (string)streamReader.ReadToEnd();
} catch (Exception e) {
return "Exception: " + e;
}
}
}
UPDATE
Request from Webbrowser:
GET /myproject/public/client/register?identity=LO0dzILO0E5ULO0IBXaW5kb3dzIEDUNLO0JMQUNLO01pY3Jvc29mdCBXaW5kb3dzIE5UIDYuMi45MjAwLjA= HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: de,en-US;q=0.7,en;q=0.3 Client-Ip: 147.153.195.175 Connection: keep-alive Cookie: XSRF-TOKEN=eyJpdiI6IkhPNW9yeVhkNGFiQWFnWUtFQ2FRREE9PSIsInZhbHVlIjoiZm1yR1NxN0tvTEFJSTNoYnJWdmNRWWZEVTRDOWVYUHJNemhDWCs4TFY4VnNoU2J5ZXNha3dPNHAwNHlKc0lHMlRGQUdiUE9BdnJcL2NDb1l4RytYSFJRPT0iLCJtYWMiOiI2ODliODM1ZDU4NzFlMTcxNmUyNGMzYjNhYTBkNGU0NjI3NDQ4MmYxZTFiZTg5YWNjMmEyZWFiMDliYWExZjY4In0%3D; laravel_session=eyJpdiI6Ijl0Mnlid0xkdTRRQVh2UDZhXC95MzFBPT0iLCJ2YWx1ZSI6InR4eCtTRVg5XC81YTU3dHB1azluVHpYemw5a2ZkbnBEMGdsRllGSXFIajFJSUdWcitwSTFQS1pWXC9LUlR3Vzh0eExSY3Via2xwOTBhVG5kZXVzRXdWbGc9PSIsIm1hYyI6Ijg5MmZiNDMxOTFhMGQzNjMxNmM0NzI1NzAyOTk3ZTU4ODkwMzdkYzM3YTIxZTU3M2MzMWYzYjM5NTI1OWVhZDQifQ%3D%3D Dnt: 1 Host: localhost Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0 Via: 223.100.9.3 X-Forwarded-For: 36.2.247.179
Request from C# Application:
GET /myproject/public/client/register?identity=LO0dzILO0E5ULO0IBXaW5kb3dzIEDUNLO0JMQUNLO01pY3Jvc29mdCBXaW5kb3dzIE5UIDYuMi45MjAwLjA= HTTP/1.1
Host: localhost
Hint: I changed the base64 string to something else...
UPDATE
I changed my Network class to this, so I can attach an empty cookieContainer to the request. But it does not change the request at all.
public static class Network
{
public static string sendWebRequest(string URL)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.CookieContainer = new CookieContainer();
try {
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.IO.StreamReader streamReader = new System.IO.StreamReader(response.GetResponseStream());
return (string)streamReader.ReadToEnd();
} catch (Exception e) {
return "Exception: " + e;
}
}
}
I solved it by adding a webBrowser element to my C# Form and calling the URL from the browser like this:
webBrowser1.Navigate("http://localhost/myproject/public/client/register?identity=" + getIdentity());
I created a class in Windows Form application that uses HttpWebResponse.
This class gets the response from some page before login and after login and I analyze this pages.
Everything is good working at all time.
BUT
I used this class in a web application and uploaded project files in server.
I get good response from all page EXCEPT login page. Login page response contains unclear characters like this:
� �Z�N#G�v��CM�'��X؎2�LvV�e�H{��݅����t�
(ڋa���z.��2�͒H��膇
�+�9U�v�n��$pW��S�|��s�)=z����ߗ����0*T��~JuF5y�Ǟ���D��
and it happens often, that means I get sometimes normal response.
For example when extract my zip file again in server, it may work fine.
I am confused, I can't tell regular rule.
URL = "http://www.nonsense-website.com/login";
var request2 = (HttpWebRequest)WebRequest.Create(URL);
string username1 = jandoe;
string password1 = pass1234;
string postData = "hidlogin=1&username=" + username1 + "&password=" + password1;
var data = Encoding.UTF8.GetBytes(postData);
request2.Method = "POST";
request2.ContentType = "application/x-www-form-urlencoded";
request2.ContentLength = data.Length;
request2.UserAgent = "Mozilla/5.0 (Windows NT 10.0; rv:55.0) Gecko/20100101 Firefox/55.0";
request2.Headers.Add("Cookie:" + cookie);
request2.Headers.Add("Accept-Language: en-US,en;q=0.5");
request2.Headers.Add("Accept-Encoding: gzip, deflate");
request2.KeepAlive = true;
request2.Headers.Add("Upgrade-Insecure-Requests: 1");
using (var stream = request2.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
try
{
using (var response2 = (HttpWebResponse)request2.GetResponse())
{
responseString = new StreamReader(response2.GetResponseStream(),Encoding.UTF8).ReadToEnd();
AnalyzeResponse(responseString); // other function that analyze response data
}
}
catch (Exception ex)
{
WriteData("Exception in Login" + Environment.NewLine + ex.Message); // write in file
}
and my exception:
Exception in Login
Error in Parse the HTML
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
You're accepting gzip and deflate compressed responses (via Accept-Encoding), but you aren't checking whether the response is compressed. Turn on automatic decompression to have this done for you:
request2.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
I'm trying to automate configuring a wireless router's SSID and Password via c# webclient. The router has no API that I know of. It's an unbranded chinese router. The web config seems to be the only option for configuration. It uses http-basic-authentication (you browse to the IP address of the router and get a generic dialog asking for username and password).
I've used Wireshark to get the headers and form fields that the http-post requests use when I manually update the SSID and Password (two separate forms). I then attempted to use webclient to emulate those post requests.
Here is a snippet of code that I am using to attempt to save a new SSID (NameValueCollection is defined elsewhere):
private const string FORM_SSID = "http://192.168.1.2/formWlanSetup.htm";
private const string REF_SSID = "http://192.168.1.2/formRedirect.htm?redirect-url=wlbasic.htm&wlan_id=0";
private NameValueCollection mFields = HttpUtility.ParseQueryString(string.Empty, Encoding.ASCII);
public string SaveConfigResponse()
{
try
{
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.Accept] = "text/html, application/xhtml+xml, */*";
wc.Headers[HttpRequestHeader.Referer] = REF_SSID;
wc.Headers[HttpRequestHeader.AcceptLanguage] = "en-US";
wc.Headers[HttpRequestHeader.UserAgent] = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
wc.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
wc.Headers[HttpRequestHeader.Host] = "192.168.1.2";
wc.Headers[HttpRequestHeader.Connection] = "Keep-Alive";
wc.Headers[HttpRequestHeader.ContentLength] = Encoding.ASCII.GetBytes(mFields.ToString()).Length.ToString();
wc.Headers[HttpRequestHeader.CacheControl] = "no-cache";
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(config_user + ":" + config_pass));
wc.Headers[HttpRequestHeader.Authorization] = string.Format("Basic {0}", credentials);
//wc.Credentials = new NetworkCredential("admin", "admin");
return Encoding.ASCII.GetString(wc.UploadValues(FORM_SSID, "POST", mFields));
}
}
catch (Exception ex)
{
return ex.Message;
}
}
This results in an http-status-code-401 not authorized response. Is what I'm trying to do just impossible?
UPDATE
Here are the HTTP headers of both the browser post/response and the WebClient post/response. Again, I tried to match what I saw the browser posting as well as I could with my WebClient post.
Browser:
POST /formWlanSetup.htm HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://192.168.1.2/formRedirect.htm?redirect-url=wlbasic.htm&wlan_id=0
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
Host: 192.168.1.2
Content-Length: 524
Connection: Keep-Alive
Cache-Control: no-cache
Authorization: Basic YWRtaW46YWRtaW4=
HTTP/1.1 302 Found
Location: wlbasic.htm
Content-Length: 183
Date: Thu, 23 Oct 2014 18:18:27 GMT
Server: eCos Embedded Web Server
Connection: close
Content-Type: text/html
Transfer-Encoding: chunked
Cache-Control: no-cache
WebClient:
POST /formWlanSetup.htm HTTP/1.1
Accept-Language: en-US
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
Authorization: Basic YWRtaW46YWRtaW4=
Accept: text/html, application/xhtml+xml, */*
Content-Type: application/x-www-form-urlencoded
Referer: http://192.168.1.2/formRedirect.htm?redirect-url=wlbasic.htm&wlan_id=0
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: 192.168.1.2
Content-Length: 524
Connection: Keep-Alive
HTTP/1.1 401 Not Authorized
WWW-Authenticate: Basic realm="AP"
Date: Thu, 23 Oct 2014 18:18:41 GMT
Server: eCos Embedded Web Server
Connection: close
Content-Type: text/html
Transfer-Encoding: chunked
Cache-Control: no-cache
Again, that was all gleaned from Wireshark. I'm not very familiar with Wireshark, but I was able to get this far. If I knew how to properly extract the raw packet data and pastebin it, I would.
Important New Observations
The Wireshark captures of the post packets from both Browser and WebClient obviously differ in the order of the headers. I don't know how significant that might or might not be, though, as the data for each header is clearly the same.
One stark difference between the packets that I noticed is that Wireshark reports the Browser packet to be significantly larger than the WebClient packet. Looking at the itemized view, I couldn't find any obvious differences. I assume posting raw data for comparison would reveal a lot, but again, I don't really know how to do that.
I had a bewildering revelation. Despite the response clearly stating '(401) Unauthorized', the post is in fact being accepted by the router! Driving in to the router's web config after my WebClient post shows that the settings were accepted and saved.
That last one is a biggie. I find myself in a situation where I can get my config to save with a WebClient post, but I have to ignore a 401 response in order to do so. Obviously, this is far from ideal. So close, yet so far!
FINAL UPDATE (RESOLUTION)
I've solved the issue of failing basic authentication, though not with WebClient. I used the suggestion from #caesay and went with HttpWebRequest (together with WebResponse). My form posts result in redirects, so I had to allow for that.
This is essentially what I went with:
private bool ConfigureRouter()
{
bool passed = false;
string response = "";
HttpWebRequest WEBREQ = null;
WebResponse WEBRESP = null;
// Attempt to POST form to router that saves a new SSID.
try
{
var uri = new Uri(FORM_SSID); // Create URI from URL string.
WEBREQ = HttpWebRequest.Create(uri) as HttpWebRequest;
// If POST will result in redirects, you won't see an "OK"
// response if you don't allow those redirects
WEBREQ.AllowAutoRedirect = true;
// Basic authentication will first send the request without
// creds. This is protocol standard.
// When the server replies with 401, the HttpWebRequest will
// automatically send the request again with the creds when
// when PreAuthenticate is set.
WEBREQ.PreAuthenticate = true;
WEBREQ.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
// Mimic all headers known to satisfy the request
// as discovered with a tool like Wireshark or Fiddler
// when the form was submitted from a browser.
WEBREQ.Method = "POST";
WEBREQ.Accept = "text/html, application/xhtml+xml, */*";
WEBREQ.Headers.Add("Accept-Language", "en-US"); // No AcceptLanguage property built-in to HttpWebRequest
WEBREQ.UserAgent = USER_AGENT;
WEBREQ.Referer = REF_SSID;
WEBREQ.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
WEBREQ.KeepAlive = true;
WEBREQ.Headers.Add("Pragma", "no-cache"); // No Pragma property built-in to HttpWebRequest
// Use a cached credential so that the creds are properly
// submitted with subsequent redirect requests.
CredentialCache creds = new CredentialCache();
creds.Add(uri, "Basic", new NetworkCredential(config_user, config_pass));
WEBREQ.Credentials = creds;
// Submit the form.
using (Stream stream = WEBREQ.GetRequestStream())
{
SSID ssid = new SSID(ssid_scanned); // Gets predefined form fields with new SSID inserted (NameValueCollection PostData)
stream.Write(ssid.PostData, 0, ssid.PostData.Length);
}
// Get the response from the final redirect.
WEBRESP = WEBREQ.GetResponse();
response = ((HttpWebResponse)WEBRESP).StatusCode.ToString();
if (response == "OK")
{
StatusUpdate("STATUS: SSID save was successful.");
passed = true;
}
else
{
StatusUpdate("FAILED: SSID save was unsuccessful.");
passed = false;
}
WEBRESP.Close();
}
catch (Exception ex)
{
StatusUpdate("ERROR: " + ex.Message);
return false;
}
return passed;
}
Is what I'm trying to do just impossible?
No, its not impossible. I have had many headaches with web scraping like this over the years because some web servers are picky, and your router interface is likely a custom web server implementation that isnt as forgiving as apache or iis.
I would do a wireshark capture and get the raw packet data that chrome sends (w/ payload etc), and then do the same capture for your application. Make sure the packets are as similar as you can get them. If you still have issues, post the packet captures to pastebin or something so we can have a look.
EDIT::
Instead of using the limited WebClient API, try using some lower level items, I wonder if the following code will work for you:
var uri = new Uri("http://192.168.1.2/formWlanSetup.htm");
var cookies = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.CookieContainer = cookies;
request.ServicePoint.Expect100Continue = false;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
request.Referer = "http://192.168.1.2/formRedirect.htm?redirect-url=wlbasic.htm&wlan_id=0";
request.Credentials = new NetworkCredential(config_user, config_pass);
request.PreAuthenticate = true;
var response = request.GetResponse();
var reader = new StreamReader(response.GetResponseStream());
string htmlResponse = reader.ReadToEnd();
I built a C# application about 8 months ago to pull SurveyMonkey responses and store them on our SQL Server. The application has run every day for over 6 months without any issue, but suddenly Friday morning I can't get any requests to go through. I'm not seeing anything on the developer site mentioning outages, so I've been examining my code for possible issues.
This is the first app I've ever written to create web requests, so it's possible I'm doing things badly.
Request, from Fiddler:
POST https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=<key hidden> HTTP/1.1
Authorization: bearer <token hidden>
Content-Type: application/json
Host: api.surveymonkey.net
Content-Length: 146
Expect: 100-continue
Connection: Keep-Alive
{
"page": 1,
"fields": ["title","analysis_url","preview_url","date_created","date_modified","language_id","question_count","num_responses"]
}
Response body, from Fiddler:
{"status":1,"errmsg":"Request header \"Authorization\" token not found"}
C# code:
private JObject SubmitPostRequest(string URIPath, string RequestBody)
{
using (var webClient = new WebClient())
{
// Create URI for POST request to go to. URI varies depending on API method and includes API Key.
Uri requestURI = new Uri(APIHost, URIPath + "?api_key=" + APIKey);
// Have web client use NT credentials for proxy server
webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
// Specify headers: access token in authorization header, content type
webClient.Headers["Authorization"] = "bearer " + AccessToken;
webClient.Headers["Content-Type"] = "application/json";
// Create requestBody as byte[]
byte[] requestBody = Encoding.Default.GetBytes(RequestBody);
// Connect to the URI and send requestBody as a POST command.
byte[] postResponse = webClient.UploadData(requestURI, "POST", requestBody);
// Update LastConnect
LastConnect = DateTime.Now;
++TransactionCounter;
// Convert byte[] response to string, parse string and return as JObject.
JObject jsonResponse = JObject.Parse(Encoding.Default.GetString(postResponse));
// Evaluate "status" field of jsonResponse. Throw exception if response is not 0.
dynamic dyn = jsonResponse;
if (dyn.status != 0)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("HTTP Post request failed:");
sb.Append("\tstatus: ").AppendLine(dyn.status.ToString());
sb.Append("\terrmsg: ").AppendLine(dyn.errmsg.ToString());
throw new WebException(sb.ToString());
}
return jsonResponse;
}
}
It took awhile, but I managed to get in touch with someone in corporate IT who monitored the traffic to see what was going on. They implemented a fix to the proxy server and everything is finally working again.
UPDATED
See post #3 below.
There is a need to upload a file to the web automatically (without browser). Host - Mini File Host v1.2 (if this does matter). Didn't find specific api in documentation, so at first i sniffed browser requests in Firebug as follows :
Params : do
Value : verify
POST /upload.php?do=verify HTTP/1.1
Host: webfile.ukrwest.net
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; ru; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 (.NET CLR 4.0.20506)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ru,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://filehoster.awardspace.com/index.php
Content-Type: multipart/form-data; boundary=---------------------------27368237179714
Content-Length: 445
-----------------------------27368237179714
Content-Disposition: form-data; name="upfile"; filename="Test.file"
Content-Type: application/octet-stream
12345678901011121314151617sample text
-----------------------------27368237179714
Content-Disposition: form-data; name="descr"
-----------------------------27368237179714
Content-Disposition: form-data; name="pprotect"
-----------------------------27368237179714--
Here we can see parameter, headers, content type and chunks of information (1 - file name and type, 2 - file contents, 3 - additional params - description and password, not necessarily applied).
So i've created a class that emulates such a behaviour step by step : HttpWebRequest on the url, apply needed parameters to request, form request strings with StringBuilder and convert them to byte arrays, read a file using FileStream, putting all that stuff to MemoryStream and then writing it to request (took major part of a code from an article at CodeProject where it uploads a file to Rapidshare host).
Neat and tidy, but... It doesn't seem to work :(. As result it returns initial upload page, not a result page with links i could parse and present to a user...
Here are main methods of an Uploader class :
// Step 1 - request creation
private HttpWebRequest GetWebrequest(string boundary)
{
Uri uri = new Uri("http://filehoster.awardspace.com/index.php?do=verify");
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
httpWebRequest.CookieContainer = _cookies;
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; ru; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 (.NET CLR 4.0.20506)";
httpWebRequest.Referer = "http://filehoster.awardspace.com/index.php";
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
httpWebRequest.Timeout = -1;
//httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
httpWebRequest.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7");
httpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
httpWebRequest.Headers.Add("Accept-Language", "ru,en-us;q=0.7,en;q=0.3");
//httpWebRequest.AllowAutoRedirect = true;
//httpWebRequest.ProtocolVersion = new Version(1,1);
//httpWebRequest.SendChunked = true;
//httpWebRequest.Headers.Add("Cache-Control", "no-cache");
//httpWebRequest.ServicePoint.Expect100Continue = false;
return httpWebRequest;
}
// Step 2 - first message part (before file contents)
private string GetRequestMessage(string boundary, string FName, string description, string password)
{
System.Text.StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("--");
stringBuilder.Append(boundary);
stringBuilder.Append("\r\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"");
stringBuilder.Append("upfile");
stringBuilder.Append("\"; filename=\"");
stringBuilder.Append(FName);
stringBuilder.Append("\"");
stringBuilder.Append("\r\n");
stringBuilder.Append("Content-Type: application/octet-stream");
stringBuilder.Append("\r\n");
return stringBuilder.ToString();
}
// Step 4 - additional request parameters. Step 3 - reading file is in method below
private string GetRequestMessageEnd(string boundary)
{
System.Text.StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(boundary);
stringBuilder.Append("\r\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"descr\"");
stringBuilder.Append("\r\n");
stringBuilder.Append("\r\n");
stringBuilder.Append("Default description");
stringBuilder.Append("\r\n");
stringBuilder.Append(boundary);
stringBuilder.Append("\r\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"pprotect\"");
stringBuilder.Append("\r\n");
stringBuilder.Append("\r\n");
stringBuilder.Append("");
stringBuilder.Append("\r\n");
stringBuilder.Append(boundary);
stringBuilder.Append("--");
//stringBuilder.Append("\r\n");
//stringBuilder.Append(boundary);
//stringBuilder.Append("\r\n");
return stringBuilder.ToString();
}
// Main method
public string ProcessUpload(string FilePath, string description, string password)
{
// Chosen file information
FileSystemInfo _file = new FileInfo(FilePath);
// Random boundary generation
DateTime dateTime2 = DateTime.Now;
long l2 = dateTime2.Ticks;
string _generatedBoundary = "----------" + l2.ToString("x");
// Web request creation
System.Net.HttpWebRequest httpWebRequest = GetWebrequest(_generatedBoundary);
// Main app block - form and send request
using (System.IO.FileStream fileStream = new FileStream(_file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] bArr1 = Encoding.ASCII.GetBytes("\r\n--" + _generatedBoundary + "\r\n");
// Generating pre-content post message
string firstPostMessagePart = GetRequestMessage(_generatedBoundary, _file.Name, description, password);
// Writing first part of request
byte[] bArr2 = Encoding.UTF8.GetBytes(firstPostMessagePart);
Stream memStream = new MemoryStream();
memStream.Write(bArr1, 0, bArr1.Length);
memStream.Write(bArr2, 0, bArr2.Length);
// Writing file
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
// Generating end of a post message
string secondPostMessagePart = GetRequestMessageEnd(_generatedBoundary);
byte[] bArr3 = Encoding.UTF8.GetBytes(secondPostMessagePart);
memStream.Write(bArr3, 0, bArr3.Length);
// Preparing to send
httpWebRequest.ContentLength = memStream.Length;
fileStream.Close();
Stream requestStream = httpWebRequest.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
// Sending request
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
}
// Delay (?)
System.Threading.Thread.Sleep(5000);
// Getting response
string strResponse = "";
using (Stream stream = httpWebRequest.GetResponse().GetResponseStream())
using (StreamReader streamReader = new StreamReader(stream/*, Encoding.GetEncoding(1251)*/))
{
strResponse = streamReader.ReadToEnd();
}
return strResponse;
}
Plays with ProtocolVersion (1.0, 1.1), AllowAutoRedirect (true/false), even known ServicePoint.Expect100Continue (false) didn't fix an issue. Even a 5sec timeout before getting response (thought in case of a big file it doesn't uploads so quick) didn't help.
Content type "octet-stream" was chosen by purpose to upload any file (could use some switch for most popular jpg/zip/rar/doc etc., but that one seems universal). Boundary is generated randomly from timer ticks, not a big deal. What else? :/
I could give up and forget this, but i feel i'm pretty close to solve and then forget about it :P.
In case you need the whole application to run and debug - here it is (70kb, zipped C# 2.0 VS2k8 solution, no ads, no viruses) :
#Mediafire
#FileQube
#FileDropper
Update : nope, there is no redirect.
screenshot
Read RFC2388 few times, rewrote the code and it finally worked (i guess the trouble was in utf-read trailing boundary instead of correct 7 bit ascii). Hooray? Nope :(. Only small files are transfered, big ones throwing "The connection was closed unexpectedly".
System.Net.WebException was unhandled by user code
Message="The underlying connection was closed: The connection was closed unexpectedly."
Source="Uploader"
StackTrace:
at Uploader.Upload.ProcessUpload(String FilePath, String description, String password) in F:\MyDocuments\Visual Studio 2008\Projects\Uploader\Uploader.cs:line 96
at Uploader.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in F:\MyDocuments\Visual Studio 2008\Projects\Uploader\Form1.cs:line 45
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
I know that's a bug with .net stack and few solutions exists :
increase both Timeout and ReadWriteTimeout of a request
assign request.KeepAlive = false and System.Net.ServicePointManager.Expect100Continue = false
set ProtocolVersion to 1.0
But neither one of them nor all of them altogether help in my case. Any ideas?
EDIT - Source code:
// .. request created, required params applied
httpWebRequest.ProtocolVersion = HttpVersion.Version10; // fix 1
httpWebRequest.KeepAlive = false; // fix 2
httpWebRequest.Timeout = 1000000000; // fix 3
httpWebRequest.ReadWriteTimeout = 1000000000; // fix 4
// .. request processed, data written to request stream
string strResponse = "";
try
{
using (WebResponse httpResponse = httpWebRequest.GetResponse()) // error here
{
using (Stream responseStream = httpResponse.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
strResponse = streamReader.ReadToEnd();
}
}
}
}
catch (WebException exception)
{
throw exception;
}
"As result it returns initial upload page, not a result page with links i could parse and present to a user..."
Maybe that's just the behaviour of the upload functionality: that after the upload has finished, you can upload another file?
I think you have to call another url for the "browse for file"-page (I suppose that's the one you need).
Edit: Actually, if the server sends a "redirect" (http 3xx), that's something the browser has to handle, so if you're working with your own client application in stead of a browser, you'll have to implement redirection yourself. Here the rfc for more information.
Try setting the maxRequestLength property of the httpRuntime element in the Web.config.
In my case, duplicate filename causing the issue as well. I save the file's settings in an xml file but the name setting is duplicating each other.
<field name="StillImage">
<prefix>isp_main_</prefix>
<suffix>308</suffix>
<width>1080</width>
<height>1080</height>
</field>
<field name="ThumbnailImage">
<prefix>isp_thumb_</prefix> // pay attention to this
<suffix>308</suffix>
<width>506</width>
<height>506</height>
</field>
<field name="Logo">
<prefix>isp_thumb_</prefix> // and this
<suffix>306</suffix>
<width>506</width>
<height>506</height>
</field>
And, in the other case I had, the issue is in the file length. Please do check the allowed file size on your server. In your script just do check this part :
dataStream.Write(filesBytesArray, 0, filesBytesArray.Length);
dataStream.Close();
And if you dont know, just limit the file uploaded size in your frontend section ie. HTML <input type="file"> upload element, this is good reference for limiting file size and other filter.