|
-
Oct 8th, 2005, 01:40 PM
#1
Thread Starter
Member
WebClient VS. HttpWebRequest, HttpWebResponse to "POST" a file
Hello all;
I've been trying to post an XML file to a specified uri, we can use either webClient or httpwebrequest and response classes. does anyone know whats the difference between them in this case? both of them can give the same result? personally, i think webClient is simpler!
I have another question, how can i get the http result out of posting? the results from both webclient and webReponse returns as array of bytes. but i can't get the error code...in this case, can i assume if the posting didn't throw any exception that the result of the http is "404 Accepted" ?
Thank you for your time.
-
Oct 10th, 2005, 01:39 PM
#2
Re: WebClient VS. HttpWebRequest, HttpWebResponse to "POST" a file
WebClient is just a specific implementation of HTTPWebRequest/Response. It is a "wrapper" class. So using it is perfectly OK.
That said, I don't usually use it.
Here is the code from one of my pages that does a server-side http request:
It sends a XML document and then receives a response based on what it sent (it is querying an XML data source, similar to SOAP)
Code:
//BEGIN CODE SNIPPET. sURL, sResponse and XML are string variables
System.Net.WebRequest oRequest = System.Net.WebRequest.Create(sURL);
oRequest.Method = "POST";
System.IO.Stream oStream = oRequest.GetRequestStream();
byte[] oBytes = GetByteArray(XML);
oStream.Write(oBytes, 0, oBytes.Length);
oStream.Close();
System.Net.WebResponse oResponse = oRequest.GetResponse();
oStream = oResponse.GetResponseStream();
sResponse = "";
int iRead = 0;
while(true)
{
oBytes = new byte[2048];
iRead = oStream.Read(oBytes, 0, 2048);
if(iRead == 0) break;
sResponse += GetStringFromByteArray(oBytes);
}
//END SNIPPET
//And, my two helper functions that I use in this snippet:
public static byte[] GetByteArray(string sIn)
{
byte[] oTmp = new byte[sIn.Length];
for(int i = 0 ; i < sIn.Length ; i++)
{
oTmp[i] = (byte)(sIn.Substring(i, 1).ToCharArray(0, 1)[0]);
}
return oTmp;
}
public static string GetStringFromByteArray(byte[] oIn)
{
string sTmp = "";
for(int i = 0 ; i < oIn.Length ; i++)
{
if(oIn[i] == 0) break;
sTmp += ((char)oIn[i]).ToString();
}
return sTmp;
}
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Oct 10th, 2005, 02:07 PM
#3
Thread Starter
Member
Re: WebClient VS. HttpWebRequest, HttpWebResponse to "POST" a file
Thanks for the reply.
I already figured out this method, u can also get the HTTP status of the response if you cast the webResponse to HttpWebResponse and use the status property.
However; i am trying to do a batch posting to the server (just click on post like 10000 times per second . sometimes the HTTP status returns "Protocol Error" or "Connection closed in the underlying..something". but when u try the same with WebClient it doesn't return this error.
I have another question if you don't mind, you can set the credentials with the request by simply initialyzing a new instance of networkCredentials and set the username and password, right? If you have a sample code to receive the posted XML file from the server and save it, i would really appreciate posting it to me.
Thnx anyway
-
Oct 10th, 2005, 02:16 PM
#4
Re: WebClient VS. HttpWebRequest, HttpWebResponse to "POST" a file
The "underlying connection closed" error still occurs for the WebRequest class as well, but it is trapping it and taking different action depending on the nature of the error, including possibly resubmitting the request automatically.
Network credentials dont usually mean diddly for http requests, so that portion of your followup only confuses me...
As for saving to a file, just take the sResponse and pass it in to a file writer "System.IO.FileStream" through the GetByteArray utility function I already provided. Then, close the file.
Usually, it looks like this:
Code:
System.IO.FileStream oFile;
try
{
oFile = new System.IO.FileStream("path to file here", System.IO.FileMode.Create);
}
catch(System.IO.DirectoryNotFoundException ee)
{
//handle your error here
return;
}
byte[] bWrite = GetByteArray(sResponse);
oFile.Write(bWrite, 0, bWrite.Length);
oFile.Close();
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
-
Jan 23rd, 2009, 01:32 AM
#5
New Member
Re: WebClient VS. HttpWebRequest, HttpWebResponse to "POST" a file
Hi,
I have logged-in my server once, now i want to use HttpWebRequest to post some data, but i dont get the cached login credentails. Neither is asks for it and just fails with 401 status.
I have few questions:
1. How i can cache login credentials once user logs-in for the first time?
2. Is there a way in HttpWebRequest/WebClient to force IE to pop-up login window if 401 is received?
3. When using SHDocVw.InternetExplorer object, 401 presents log-in window, but i am not able to read the response in this case. Is it possible to get response using SHDocVw.InternetExplorer object? If Yes, how? Also when user provides login details in this case, how i can cache them for furture use?
Any help is appreciated. I am stuck up with this for past couple of days.
Many Thanks
-
Jan 24th, 2009, 01:34 PM
#6
Re: WebClient VS. HttpWebRequest, HttpWebResponse to "POST" a file
Hello, welcome to the forums. Ideally, you should have created a new thread for this question.
If the server is asking for credentials, then you need to assign to the .Credentials property of the HttpWebRequest. Declare a new NetworkCredential object, in the constructor, pass the username and password and domain name. Assign it to the Credentials property, then make your request against the web server.
I don't believe that you can force that popup to appear, because that is a browser component. IE has a different popup, Firefox has a different popup and because your code is effectively emulating a browser in a minimal way, if you want the user to enter their credentials, you will need to do this yourself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|