Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, Just wondering if you can help me with my project.

I'm currently attempting to post to a url. I'll provide as much information as possible below without giving out my actual API url out to people since it's client based.

So here is what I'm trying to do, For instance here is my api url.

https://test-to-show-you.com/API/Authentication

I've tried adding the post to the url but that doesn't work because it gives no response for example.

https://test-to-show-you.com/API/Authentication?payload=user%3Dtestusername%2Cpassword%3Dtestpassword

But when using postman google chrome app, I can post to this fine, obviously not in the url though.

and this is a POST method with Content-Type: application/x-www-form-urlencoded

Here is what I'm trying to post to the url "payload=user%3Dtestusername%2Cpassword%3Dtestpassword"

Before going any further I'd just like to say it needs to be exactly like this otherwise you get no response from the server so it needs the %3D and %2C in the post method for some strange reason.

Here is what I've currently got coded into my application.

C#
var tasks = new List<Task<string>>();
for (int i = 0; i < accoutList.Count(); i++)
{
    int g = i;
    var postParameters = new NameValueCollection();
    try
    {
        Accounts ac = accoutList[i];
        tasks.Add(Task.Factory.StartNew(() => { return GetWebResponse("https://test-to-show-you.com/API/Authenticate", g); }));
    }

    catch
    {

    }
}


So I'm just really stuck here on how to post that exactly to the url inhand. Would appreciate some support into this :)
Posted
Comments
Kuthuparakkal 21-Feb-15 15:32pm    
Is that GET or POST ? Normally Query String redirects are GET. Open the WebSite and look at its html, you would see - method="post" - in the section where you have a form tag. If it's POST then you need to really make POST request rather than creating a query string.
https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx
Zoltán Zörgő 21-Feb-15 18:35pm    
Never ever send username-password in a GET query string!
Kuthuparakkal 22-Feb-15 16:40pm    
Try this:

public static HttpWebResponse POST(string postData, string url, string referer, CookieContainer cookie)
{
try
{
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.KeepAlive = true;
request.AllowAutoRedirect = false;
request.Accept = "*/*";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookie;
if (!string.IsNullOrEmpty(referer))
request.Referer = referer;
request.ContentLength = byteArray.Length;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
// request.Proxy = null;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
return (HttpWebResponse)request.GetResponse();
}
catch
{
return (HttpWebResponse)null;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900