Asked  7 Months ago    Answers:  5   Viewed   28 times

I need to use "HTTP Post" with WebClient to post some data to a specific URL I have.

Now, I know this can be accomplished with WebRequest but for some reasons I want to use WebClient instead. Is that possible? If so, can someone show me some example or point me to the right direction?

 Answers

10

I just found the solution and yea it was easier than I thought :)

so here is the solution:

string URI = "http://www.myurl.com/post.php";
string myParameters = "param1=value1&param2=value2&param3=value3";

using (WebClient wc = new WebClient())
{
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
    string HtmlResult = wc.UploadString(URI, myParameters);
}

it works like charm :)

Tuesday, June 1, 2021
 
rblarsen
answered 7 Months ago
31

Try following one

    $data = '{"id": "6"}';

    $url = "https://test.firebaseio.com/test_api/types.json";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);                               
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
    $jsonResponse = curl_exec($ch);
    if(curl_errno($ch))
    {
        echo 'Curl error: ' . curl_error($ch);
    }
    curl_close($ch);

Firebase accept json object and you have to post $data as json object. And you can use Content-Type: application/x-www-form-urlencoded or Content-Type: text/plain

Saturday, May 29, 2021
 
Daveel
answered 7 Months ago
65

I used:

QByteArray postData;
postData.append("param1=string&");
postData.append("param2=string");

So & instead of newline after each parameter.

Monday, June 21, 2021
 
TecHunter
answered 6 Months ago
68

If you want to monitor the progress of an upload, you'll need to use UploadFileAsync instead of UploadData.

With UploadDataAsync you are supposed to manually chunk the file and display the progress (at least, that's what I've determined from my own experience in the matter though I haven't seen it written as such on MSDN).

What you're looking for is to use UploadFileAsync instead, which will call the UploadProgressChanged event correctly. You can then view the event args properties BytesSent and TotalBytesToSend which should be reflected correctly.

I assume the rationale behind this is that when you're sending data, you can loop over chunks of your data stream and manually increment your progress tracker whereas with a file you cannot (.NET will manage the entire upload for you). Personally, I feel there is something fishy because there is no reason for the UploadProgressChanged event to be called with invalid information in case of UploadDataAsync - either it is called with valid, correct info or it's not called at all.

At any rate, give UploadFileAsync a shot and see how that goes.

Thursday, August 5, 2021
 
bancer
answered 4 Months ago
44

It seems you found a bug in WebClient/WebRequest, though perhaps Microsoft put that in intentionally, who knows. Nonetheless, when you pass in TW., the URI class is translating that to TW without the period. Since WebClient/WebRequest parse strings into URI, your . is disappearing in that world.

You may have to use TcpClient to get around this and roll your own web client. Any variation of this:

TcpClient oClient = new TcpClient("www.shareprice.co.uk", 80);

NetworkStream ns = oClient.GetStream();

StreamWriter sw = new StreamWriter(ns);
sw.Write(
   string.Format( 
      "GET /{0} HTTP/1.1rnUser-Agent: {1}rnHost: www.shareprice.co.ukrnrn",
           "TW.", 
           "MyTCPClient"  )
);                    
sw.Flush();

StringBuilder sb = new StringBuilder();

while (true)
{
    int i = ns.ReadByte(); // Inefficient but more reliable 
    if (i == -1) break;  // Other side has closed socket 
    sb.Append( (char) i );   // Accrue 'c' to save page data 
}

oClient.Close();

This will give you a 302 redirect, so just parse out the 'Location:' and execute the above again with the new location.

HTTP/1.1 302 Found
Date: Wed, 11 Nov 2009 19:29:27 GMT
Server: lighttpd
X-Powered-By: PHP/5.2.4-2ubuntu5.7
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: /TW./TAYLOR-WIMPEY-PLC
Content-type: text/html; charset=UTF-8
Content-Length: 0
Set-Cookie: SSID=668d5d0023e9885e1ef3762ef5e44033; path=/
Vary: Accept-Encoding
Connection: close
Friday, August 27, 2021
 
CodeCaster
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share