How to translate this sample POST request into and actual browser URL

derekclee
derekclee used Ask the Experts™
on
I am able to code this in C# into a Consuming Web Service and parse the XML response. But can't it be transformed into a simple URI and be fed straight into a browser? If so what is the exact syntax of the URI?

POST /TestPublish/PutData.asmx/getMemberData HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

strUsername=fred&strPassword=fredpass
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
If you send an URL to a server it's always a get so the server need to parse the get attributes to get the values.

The Url would be:
[Base URL of the webserver]/TestPublish/PutData.asmx/getMemberData?strUsername=fred&strPassword=fredpass

But if the server doe not parse get values but only waits for post variables this will not work.
You could create a dynamic web page on your webserver that takes the get values and posts the values to the other server.

Author

Commented:
Yes, that sounds reasonable but when I enter the following URL:

http://localhost/TestPublish/PutData.asmx/getMemberData?strUsername=fred&strPassword=fredpass

I get the following error:

Server Error in '/TestPublish' Application.
--------------------------------------------------------------------------------

Request format is unrecognized for URL unexpectedly ending in '/getMemberData'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/getMemberData'.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace:


[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/getMemberData'.]
   System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +405881
   System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
   System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +193
   System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Can you help on this one?

Cheers,
DL


 
Hmm, can you post the code of the PutData.asmx
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Author

Commented:
Presently the code is at the prototype stage with no error trapping or resource disposal, nevertheless, when run using 'view in browser' using the built-in test page (which seems to allow only a POST method) it works perfectly. It is only when I put the URL in directly that I get the error.

Cheers

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
 
/// <summary>
/// Summary description for PutData
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class PutData : System.Web.Services.WebService {
 
    public PutData () 
    {
    }
 
    [WebMethod]
    public string getMemberData(string strUsername, string strPassword)
    {
        string strCon = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
        SqlConnection cn = new SqlConnection(strCon);
 
        StringBuilder sbSQL = new StringBuilder();
        sbSQL.Append("SELECT username, password, country FROM staff WHERE username = '");
        sbSQL.Append(strUsername);
        sbSQL.Append("' AND password = '");
        sbSQL.Append(strPassword);
        sbSQL.Append("'");
        SqlDataAdapter adUsers = new SqlDataAdapter(sbSQL.ToString(), cn);
        DataSet dsUsers = new DataSet();
        adUsers.Fill(dsUsers, "users");
 
        if (dsUsers.Tables[0].Rows.Count == 1)
        {
            sbSQL.Length = 0;
            sbSQL.Append("SELECT * FROM reports WHERE poss_country = '");
            sbSQL.Append(dsUsers.Tables[0].Rows[0][2]);
            sbSQL.Append("'");
            SqlDataAdapter adReports = new SqlDataAdapter(sbSQL.ToString(), cn);
            DataSet dsReports = new DataSet();
            adReports.Fill(dsReports, "reports");
 
            if (dsReports.Tables[0].Rows.Count > 0)
            {
                string strReports = datasetToString(dsReports);
                return strReports;
                //return dsReports;
            }
            else
            {
                DataSet dsReturnError = getError(2);
                string strError2 = datasetToString(dsReturnError);
                return strError2;
                //return dsReturnError;
            }
        }
        else
        {
            DataSet dsReturnError = getError(1);
            string strError1 = datasetToString(dsReturnError);
            return strError1;
            //return dsReturnError;
        }
    }
 
    public DataSet getError(int intID)
    {
        string strCon = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
        SqlConnection cn = new SqlConnection(strCon);
 
        string strSQL = "SELECT message FROM error_messages WHERE id = " + intID;
        SqlDataAdapter adError = new SqlDataAdapter(strSQL, cn);
        DataSet dsError = new DataSet();
        adError.Fill(dsError, "Error");
        return dsError;
    }
 
    public string datasetToString(DataSet dsInDataSet)
    {
        DataTable dt = dsInDataSet.Tables[0];
        StringBuilder sb = new StringBuilder();
 
        for (int i = 0; i <= dt.Columns.Count - 1; i++)
        {
            sb.Append(dt.Columns[i]);
 
            if (i < dt.Columns.Count - 1)
            {
                sb.Append(",");
            }
        }
 
        sb.Append("\n");
 
        foreach (DataRow dr in dt.Rows)
        {
            for (int i = 0; i <= dt.Columns.Count - 1; i++)
            {
                if (!Convert.IsDBNull(dr[i]))
                {
                    sb.Append(dr[i].ToString());
                }
 
                if (i < dt.Columns.Count - 1)
                {
                    sb.Append(",");
                }
            }
        }
 
        sb.Append("\n");
 
        return sb.ToString();
    }
}

Open in new window

Author

Commented:
Apologies, that was the code in the PutData.cs CodeBehind file. The code of the PutData.asmx file is below.
<%@ WebService Language="C#" CodeBehind="~/App_Code/PutData.cs" Class="PutData" %>

Open in new window

http://stackoverflow.com/questions/703869/how-to-use-httpwebrequest-to-call-a-webmethod-that-takes-in-a-byte-parameter

please read answer 2.

You code is to create a WebService not to create something that is used with get method.
So you should code a client that is using the web service.
After creating the client you can create another page that is taking the get parameters and then calling the service using the client.

Author

Commented:
Grateful for your help so far but still missing something. Looking at it from the other side, the following URL calls a web service directly through the .asmx file using parameters and gets a response in XML format which is exactly what I am trying to do. What is missing in my code?

 http://www.webservicex.net/globalweather.asmx/GetWeather?CityName=Cork&CountryName=Ireland

Thanks
Please check if get is enabled on your system:
http://support.microsoft.com/kb/819267
What do you see if you go to:
http://localhost/TestPublish/PutData.asmx

It should list you the possible methods.

Author

Commented:
Great. That was it. When I added that code to the web.config file it all worked.
Many thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial