I can't send post data to another server - asp.net

this is my code right now:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
System.Net.ServicePointManager.Expect100Continue = False
Dim client = New System.Net.WebClient()
Dim postdata = New System.Collections.Specialized.NameValueCollection
postdata.Add("username", "qweqwe")
postdata.Add("password", "asdasd")
Dim bytes = client.UploadValues("https://juzcode.com/post.php", postdata) ' exception here
Response.Write(Encoding.ASCII.GetString(bytes))
End Sub
However I'm getting exception "Unable to connect to the remote server". Am I doing something wrong?
Btw this is the code for http://juzcode.com/post.php:
<?php if($_POST["username"]==="qweqwe" && $_POST["password"]==="asdasd")echo "<b>success</b>";else echo "<i>failed</i>";

Here is an example on the web of using c# to post to PHP:
http://blog.brezovsky.net/en-text-1.html

Put your code into a Try / Catch you may be trying to fight a loosing battle behind a corporate firewall.
Check the exception.

Related

How to represent the URL as it is in VB

I want to pass the exception Url as a string to a method that I have created , so that it shows the exception Url in the mail (that is sent).
I have done the following in my ApplicationError() event in my Global.asax
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
Dim lastException As Exception = Server.GetLastError()
If (lastException Is Nothing) Then
Return
End If
Dim context As HttpContext = HttpContext.Current
Dim myMethodforMail As New myMethodforMail()
Dim exUrl As String = context.Request.Url.ToString()
Dim webServerName As String = Server.MachineName
myMethodforMail.HandleApplicationError(exUrl, webServerName)
Server.ClearError()
End Sub
But the url is printed as http://localhost:22035/examplepage.aspx?PageNumber!0331
instead of http://localhost:22035/examplepage.aspx?PageNumber=650331
could anyone kindly help me how to decode the ! in the Url and represent the Url as it is, please . ThankYou.

Aspx page deployment on IIS V7 Postback redirect to another page doesn't work

I Have 2 Pages of ASP V4.5 which are Login.aspx as a starting page and contentMainpage.aspx.
It's working when I've tested on debug mode in visual studio 2012, but when I try to deploy it on IIS the button log in is not responding redirect to another page.
The problem can be on web configuration
I try to google it for 2 days and can not get the solution right.
Please help.
here the code bellow:
Imports PurchaseOrderList.Class1
Public Class Login
Inherits System.Web.UI.Page
Dim abmPO As New ServiceReference1.Service1SoapClient
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session("LoginName") = ""
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If IsPostBack Then
Dim LoginAutentif As New PurchaseOrderList.Class1
If (LoginAutentif.ValidatePassword(Trim(TxtLoginName.Text), Trim(TxtPassW.Text))) = 0 Then
LblErrorMessage.Visible = True
LblErrorMessage.Text = "Invalid User Name or User Password"
Exit Sub
Else
If (Response.IsClientConnected) Then
Response.Redirect("~/PurchaseOrderHeaders.aspx", False)
Session("LoginName") = TxtLoginName.Text
End If
End If
End If
Catch ex As Exception
End Try
End Sub
End Class
Maybe try to "return Response.Redirect". I had this problem in MVC5.
Your issue is most likely to do with this line:
If (Response.IsClientConnected) Then
Quick google suggests there may be a few issues with IIS7/7.5 and using this property. Also I don't think it's really necessary here, it would make more sense being used in a long running routine to detect a client disconnect or timeout.
Try changing you button click to the following and see how you go.
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If IsPostBack Then
Dim LoginAutentif As New PurchaseOrderList.Class1
If (LoginAutentif.ValidatePassword(Trim(TxtLoginName.Text), Trim(TxtPassW.Text))) = 0 Then
LblErrorMessage.Visible = True
LblErrorMessage.Text = "Invalid User Name or User Password"
Exit Sub
Else
Session("LoginName") = TxtLoginName.Text
Response.Redirect("~/PurchaseOrderHeaders.aspx", True)
End If
End If
Catch ex As Exception
End Try
End Sub

Streaming PDF works on Localhost but not on published Website

I'm currently trying to open a PDF file on my website that is located on my company's network. I had this working previously, but now for some reason it isn't working. Here is what I have:
I am using impersonation to access the file. It has domain admin privileges. This is from my web.config file (username/password are altered):
<identity impersonate="true" password="pass" userName="domain\user" />
I use this code to open the PDF in a window:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim strPath As String = CStr(Session("DWGPath"))
strPath = "file://san01/prodeng/" + Mid(strPath, 4)
strPath = Replace(strPath, "\", "/")
Dim pdfPath As String = strPath
Dim client As WebClient = New WebClient()
Dim buffer As Byte() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.End()
Catch exa As WebException
Response.Redirect("DrawingError.aspx")
Catch ex As Exception
Throw ex
End Try
End Sub
This doesn't work. It redirects me to the "DrawingError.aspx" page. This link dispalys the "Session("DWGPath")" variable. I can take this variable and paste it in to my browser and the PDF opens without problem.
However, if I alter my code to this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim strPath As String = CStr(Session("DWGPath"))
Dim pdfPath As String = strPath
Dim client As WebClient = New WebClient()
Dim buffer As Byte() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.End()
Catch exa As WebException
Response.Redirect("DrawingError.aspx")
Catch ex As Exception
Throw ex
End Try
End Sub
It still doesn't work.
The account also has full control privileges to the folder that contains the PDFs.
Any help or insight would be appreciated. Thank you!
EDIT: IF I throw exa then I get this:
The account used is a computer account. Use your global user account or local user account to access this server.
I assume you're running IIS. Go to the application pool for this app and change the identity that it's running under to be the domain\user account. See if that fixes your problem.
You want to make sure that the password on this account doesn't change or else it will fail when the password expires.

Make asp.net webpage respond to WebClient request

I have run into a problem in my asp.net and vb.net programming. I have made a website which connects to a database to authenticate users. I have made another page which my windows form application uses to authenticate users so the program does not have to connect directly to the MySql Database as this is insecure.
So far, the code for my asp.net page is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
If Request.QueryString.Keys(0) = "key" And Request.QueryString.Keys(1) = "getapikeyv1" And Request.QueryString.Keys(2) = "authapienc2" Then
Dim key As String = Request.QueryString.Get("key")
If key = "dua9r3rqpK{WPDWp9r93af03r8yqrEPIF{A}W~DW)D08q3raewdawdug9q8wdyaw9wq" Then
returnLabel.Text = "XYeJay4XVh6amvf1kJ34Y6y6hR0sSokP9oJFsi0Mxl4QB0T86W0iac9o2nRfCmFmsONq6sPz+rdX+/NqFk3inHRGnwnqf0IsQXE/M/SvnvY="
Else
returnLabel.Text = "invk"
End If
Else
returnLabel.Text = "invalidrequest"
End If
End If
End Sub
The default value for 'returnLabel' is INVALIDREQUEST. My api keys defined above are working fine so are not needed to be considered here. However, when I use the following code to communicate with the webpage, it always returns with returnLabel's value as 'INVALIDREQUEST'. Even if I put the returnLabel.Text = "1233" at the beginning of the Me.Load sub, the page still returns INVALIDREQUEST when I communicate with it through my program!
I use this code to request:
Try
Dim APIRequest As New WebClient
APIRequest.QueryString.Add("key", r9j0wqfha0fh0wf0.DecryptString128Bit(APIKey5, dVadaihwdq93ra0w0))
APIRequest.QueryString.Add("getapikeyv1", "")
APIRequest.QueryString.Add("authapienc2", "")
Dim ako39rqwfa As String = APIRequest.DownloadString(EncryptionPageUrl)
MsgBox(ako39rqwfa)
Dim m As Match = Regex.Match(ako39rqwfa, "<span id=""returnLabel"">.*?</span>")
APIKey00 = m.Value.Replace("<span id=""returnLabel"">", "").Replace("</span>", "")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
As you can see, the WebClient I use is fine but the Me.Load sub in the webpage is not responding the request made by the program...
Is there any way I can make the webpage respond to the WebClient.DownloadString() function?
Thanks,
Rodit
(ps. c# or vb code would be appreciated)
Change your asp.net page to check the query string like this, not sure if you can assume they are in a certain order:
If Request.QueryString.Get("key") isnot nothing And
Request.QueryString.Get("getapikeyv1") isnot nothing And
Request.QueryString.Get("authapienc2") isnot nothing Then

Report Viewer error: The request failed with HTTP status 401: Unauthorized

I have a asp.net c# web application with contains reports in remote processing mode. I am using the report-viewer control to render the reports. When I run the application in debug mode, I'm able to view my reports however when I publish the application to a different server I get this error message:
The request failed with HTTP status 401: Unauthorized.
My report server is on a different server than the location of my published web application. I have added new role assignment to my report server and also added to my web.config but the error persists. I think I am missing something in my aspx page for reportviewer.
Any input would be appreciated.
I'm assuming you already have set the server in your codebehind such as this
reportviewer.ServerReport.ReportServerUrl = "http://{server_ip}/reportserver";
or via the properties of the report viewer control. Make sure you change {server_ip} to the actual for the report server.
Other such problems I've seen in the past have to do with access for individual reports. Since this is cross-server, you'll need a proxy user set to view the reports.
Here are 2 examples from MSDN:
Example 1
Example 2
Yes! use Page_Load & Page_Init in this way
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ReportViewer1.Visible = True
Dim strReportsFolder As String = ConfigurationManager.AppSettings("ReportsFolder")
Dim reportName As String = "report1"
ReportViewer1.ServerReport.ReportPath = strReportsFolder + reportName
End Sub
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
PopulateReport(ReportViewer1, "")
End Sub
Public Shared Sub PopulateReport(ByVal rptViewer As ReportViewer, ByVal reportName As String)
Dim strReportServer As String = ConfigurationManager.AppSettings("ReportServer")
Dim strUserName As String = ConfigurationManager.AppSettings("Username")
Dim strPassword As String = ConfigurationManager.AppSettings("Password")
Dim strDomain As String = ConfigurationManager.AppSettings("Domain")
rptViewer.ServerReport.ReportServerUrl = New Uri(strReportServer)
rptViewer.ServerReport.ReportServerCredentials = New ReportViewerCredentials(strUserName, strPassword)
End Sub

Resources