C# screen scraping processing a login control - screen-scraping

I am trying to login to a website using screen scraping and I am having difficulties.
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.2; .NET4.0C; .NET4.0E; MS-RTC LM 8)";
req.KeepAlive = false;
req.Proxy = new WebProxy(IPAddress,PORT);
req.Method = "POST";
req.Host = host;
req.Referer = referer;
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
StreamWriter requestwriter = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
requestwriter.Write(postData);
requestwriter.Close();
try
{
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
sid = sr.ReadToEnd().Trim();
}
}
}
catch (WebException e)
{
Console.WriteLine(uri.ToString());
Console.WriteLine(e.Message);
}
return sid;
}
All that I get back is the login form, it is never logging me in. Any ideas? this has been asked before, can some one help me with this...i'm stuck with the same problem

Related

Use WebBrowser control in page instead of popup for Identity Server 4 login

I am using Identity Server 4 for authentication on my WPF MVVM app. Currently, when the user wants to log in, a popup window appears with the login screen and the info gets passed back through the OidcClientOptions. However, instead of a popup window, I want the login page to show up on a WebBrowser control in the actual application. How can I accomplish this?
My Login code:
public class Login
{
private OidcClient _oidcClient = null;
LoginResult result = null;
AccessToken accessToken = new AccessToken();
public async void LoginPage()
{
var options = new OidcClientOptions()
{
Authority = "https://localhost:5001/",
ClientId = "wpf",
ClientSecret = "secret",
Scope = "openid WebAPI",
RedirectUri = "http://localhost/signin-oidc",
Flow = OidcClientOptions.AuthenticationFlow.AuthorizationCode,
Browser = new WpfEmbeddedBrowser();
};
_oidcClient = new OidcClient(options);
try
{
result = await _oidcClient.LoginAsync();
}
catch (Exception ex)
{
return;
}
if (result.IsError)
{
}
else
{
accessToken.WriteToken(result.AccessToken);
App.Current.Properties["AccessToken"] = result.AccessToken;
}
}
}
Current WpfEmbeddedBrowser I'm using:
public class WpfEmbeddedBrowser : IBrowser
{
private BrowserOptions _options = null;
public WpfEmbeddedBrowser()
{
}
public async Task<BrowserResult> InvokeAsync(BrowserOptions options, CancellationToken cancellationToken = default)
{
_options = options;
var window = new Window()
{
Width = 900,
Height = 625,
Title = "IdentityServer Login"
};
// Note: Unfortunately, WebBrowser is very limited and does not give sufficient information for
// robust error handling. The alternative is to use a system browser or third party embedded
// library (which tend to balloon the size of your application and are complicated).
var webBrowser = new WebBrowser();
var signal = new SemaphoreSlim(0, 1);
var result = new BrowserResult()
{
ResultType = BrowserResultType.UserCancel
};
webBrowser.Navigating += (s, e) =>
{
if (BrowserIsNavigatingToRedirectUri(e.Uri))
{
e.Cancel = true;
result = new BrowserResult()
{
ResultType = BrowserResultType.Success,
Response = e.Uri.AbsoluteUri
};
signal.Release();
window.Close();
}
};
window.Closing += (s, e) =>
{
signal.Release();
};
window.Content = webBrowser;
window.Show();
webBrowser.Source = new Uri(_options.StartUrl);
await signal.WaitAsync();
return result;
}
private bool BrowserIsNavigatingToRedirectUri(Uri uri)
{
return uri.AbsoluteUri.StartsWith(_options.EndUrl);
}
}

How to parse The JsonArray in windows 8.1 (for phone apps)?

1.I am trying to connect my win 8.1 phone app to my server and I succeeded and I am getting a response in JsonArray
private async void NavigateBtn_Click1(object sender, RoutedEventArgs e)
{
long phone =System.Convert.ToInt64(phoneNumber.Text);
string pwd = System.Convert.ToString(passWord.Text);
string xml = string.Empty;
Uri url = new Uri("http://www.example.com/SR/Ch?mainEvent=Login&phNum="+phone +"&pass="+pwd);
System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
var response = await httpClient.GetAsync(url);
var result = await response.Content.ReadAsStringAsync();
Debug.WriteLine(result);
JsonValue jsonList = JsonValue.Parse(result);
Debug.WriteLine(jsonList);
}
The debug.Writeline(result) output is given below
ex:[{"rV":1.2,"VerifiedUser":"test","ID":"1234","HD":"LI","Verify":"NEWUSER","key":"1234"}]
2.I want to parse the data and diplay it in text box,how to do it?
the output for jsonlist is "Windows.Data.Json.JsonValue"

System.Net.WebException: The remote server returned an error: NotFound. ---> silverlight file post to WCF Rest Service

i am trying to post an image to wcf rest service from silverlight 4 but i am getting an exception when i am taking resonse from the request, the exception is
System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.BrowserHttpWebRequest.<>c_DisplayClassa.b_9(Object sendState)
at System.Net.Browser.AsyncHelper.<>c_DisplayClass4.b_1(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at Admin.Popups.AddCategory.OnImageGetResponseCompleted(IAsyncResult ar)}
all of my other functions on the service is working fine even this one when i try to access it from android, i already had a look and tried the silverlight cross domain policy but i dont think its applicable, my code is below
private void UpLoadImage()
{
string URL = Utilities.GetServiceUrl() + "AddCategoryImage?CategoryId=2&ContentType=image&apikey=" + Utilities.GetApiKey();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "text/plain";
request.ContentLength = SelectedFile.Length;
request.BeginGetRequestStream(OnImageGetRequestStreamCompleted, request);
}
private void OnImageGetRequestStreamCompleted(IAsyncResult ar)
{
try
{
if (SelectedFile != null)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
using (Stream postStream = request.EndGetRequestStream(ar))
{
using (Stream source = SelectedFile.OpenRead())
{
byte[] buffer = new byte[2048];
int bytesRead;
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
{
postStream.Write(buffer, 0, bytesRead);
}
}
}
request.BeginGetResponse(new AsyncCallback(OnImageGetResponseCompleted), request);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void OnImageGetResponseCompleted(IAsyncResult ar)
{
try
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string result = sr.ReadToEnd();
}
}
catch (Exception ex)
{
this.Dispatcher.BeginInvoke(delegate()
{
MessageBox.Show(ex.Message);
});
}
}
the wcf function is
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "AddCategoryImage?CategoryId={CategoryId}&ContentType={ContentType}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
bool AddCategoryImage(int CategoryId, string ContentType, Stream Image);
and function implementation is
public static bool AddCategoryImage(int CategoryId, string ContentType,Stream Image)
{
SqlConnection sqlCon = new SqlConnection(Params.GetConnectionString());
SqlCommand sqlCmd = new SqlCommand("UPDATE Categories SET Cat_Img = #Cat_Img,Cat_Img_Type = #Cat_Img_Type WHERE Cat_ID = #Cat_ID", sqlCon);
sqlCmd.Parameters.Add("#Cat_Img", SqlDbType.Image).Value = Utility.ConvertStreamToByteArray(Image);
sqlCmd.Parameters.Add("#Cat_Img_Type", SqlDbType.NVarChar, 32).Value = ContentType;
sqlCmd.Parameters.Add("#Cat_ID", SqlDbType.Int).Value = CategoryId;
try
{
sqlCon.Open();
return sqlCmd.ExecuteNonQuery() > 0 ? true : false;
}
catch (Exception ex)
{
Error.CreateError(ex.Message, "Category.cs", "AddCategoryImg(int,string,Stream)");
}
finally
{
sqlCon.Close();
}
return false;
}
i have solved the issue WCF defualt maxReceivedMessageSize is 65K and i was trying to upload file more than 65k, clever me :) so i set the maxReceivedMessageSize="2000000" to 2MB now it works for files up to 2MB
<binding name ="webBinding" sendTimeout="00:10:00" receiveTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00" maxReceivedMessageSize="2000000">
<readerQuotas maxDepth="32" maxStringContentLength="100000000" maxArrayLength="100000000" maxBytesPerRead="100000000" maxNameTableCharCount="100000000" />
</binding>

HttpWebRequest "POST" returning server error (Exception)

I'm trying to call the google URL shortner API from a WP7 app, I tried first on a Console Application by doing this:
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"longUrl\":\"http://www.google.com/\"}";
Console.WriteLine(json);
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
Console.WriteLine(responseText);
}
Console.Read();
and it worked fine, and returned everything OK, but when I try to do it on a Windows Phone App like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
testConnection();
}
private void testConnection()
{
if (!NetworkInterface.GetIsNetworkAvailable())
MessageBox.Show("There's no internet connection, please reconnect to the internet and try again");
else
{
var req = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
req.ContentType = "application/json";
req.Method = "POST";
req.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), req);
textBlock2.Text = "Done";
}
}
void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
// End the stream request operation
Stream postStream = webRequest.EndGetRequestStream(asynchronousResult);
// Create the post data
// Demo POST data:
string postData = "http://www.google.com";
byte[] byteArray = Encoding.Unicode.GetBytes(postData);
// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the web request
webRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), webRequest);
}
void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response;
// End the get response operation
response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamReader = new StreamReader(streamResponse);
textBlock1.Text= streamReader.ReadToEnd();
streamResponse.Close();
streamReader.Close();
response.Close();
}
catch (WebException e)
{
}
}
The code always goes to the catch of the try method and gives "not found" error.
There is a known issue with SSL and not-trusted certificates. Connection to a web site with SSL that require ClientCertificates is not supported in the current Windows Phone 7 application model (Only BasicAuthentication is supported). You can read about the same problem here: http://forums.create.msdn.com/forums/p/65076/398730.aspx
I had a similar issue (which makes sense since I started out with this code as my example...) and when I changed the following code it started working to the point where I could progress forward again.
... from:
byte[] byteArray = Encoding.Unicode.GetBytes(postData);
... to (the equivalent of):
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

C# screen scraping processing a login control using POST

I am trying to login to a website using screen scraping and I am having difficulties.
private static string GetWebRequestContent(Uri uri)
{
string sid = String.Empty;
string userName = "username";
string password = "password";
string postData = string.Format("FormName=Login&Login={0}&Password={1}&FormAction=login&ret_page=%2FDefault.asp&querystring=",userName,password);
byte[] postBytes = Encoding.UTF8.GetBytes(postData);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 3.5.30729; InfoPath.2; .NET4.0C; .NET4.0E; MS-RTC LM 8)";
req.KeepAlive = false;
req.Proxy = new WebProxy(IPAddress,PORT);
req.Method = "POST";
req.Host = host;
req.Referer = referer;
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
StreamWriter requestwriter = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
requestwriter.Write(postData);
requestwriter.Close();
try
{
using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
sid = sr.ReadToEnd().Trim();
}
}
}
catch (WebException e)
{
Console.WriteLine(uri.ToString());
Console.WriteLine(e.Message);
}
return sid;
}
All that I get back is the login form, it is never logging me in. Any ideas?
Try monitoring what request is sent in your browser and then replicate this in your script. There are various firefox extensions that will help you do this, such as TamperData, Firebug, and LiveHttp.

Resources