How to send a string using Stream - c#

I am looking at an example of a Stream used to transfer data, I would like to pass an additional string'infoAsString'
string infoAsString = "blablabla";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(message);
request.Method = "POST";
request.ContentType = "text/xml;charset=utf-8";
request.ContentLength = requestBytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
//pass infoAsString?
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
}
//then I can grab it..
public object Upload(string infoAsString)
{
please advise....thanks for any replies

You can use request.Headers collection for this:
string infoAsString = "blablabla";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(message);
request.Method = "POST";
request.ContentType = "text/xml;charset=utf-8";
request.ContentLength = requestBytes.Length;
request.Headers.Add(string.format("infoAsString: {0}", infoAsString))
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
}

Related

HttpWebRequest with Method Post

I have this link: http://www2.correios.com.br/sistemas/rastreamento/
and need fill textarea and do subit button buscar, for that Im using this code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www2.correios.com.br/sistemas/rastreamento/ctrl/ctrlRastreamento.cfm");
request.CookieContainer = new CookieContainer();
request.Method = "POST";
string postData = "objetos=PU633524761BR";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
textBox.Text = responseFromServer;
dataStream.Close();
why dont return tracking history?
for track you can you this track number: LB250377577SE

The remote server returned an error: (403) Forbidden.. in google short url

public string GetShortURL(string longUrl)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.googleapis.com/urlshortener/v1/url?key=My_API_Key");
request.Method = "POST";
request.ContentType = "application/json";
string requestData = string.Format(#"{{""longUrl"": ""{0}""}}", longUrl);
byte[] requestRawData = Encoding.ASCII.GetBytes(requestData);
request.ContentLength = requestRawData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(requestRawData, 0, requestRawData.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader responseReader = new StreamReader(response.GetResponseStream());
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseData;
}
I am trying to create short url by passing my original url string but it is giving an exception

C# - How to send string to url in window phone?

I create method in a static class (window phone) :
public void testSend()
{
try
{
string url = "";
string str = "test";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
string Data = "data=" + str;
byte[] postBytes = Encoding.UTF8.GetBytes(Data);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream resStream = response.GetResponseStream();
var sr = new StreamReader(response.GetResponseStream());
string responseText = sr.ReadToEnd();
}
catch (WebException)
{
}
I'm not sure but it get error on GetRequestStream() and GetResponse because it does not contain in HttpWebRequest.Please help me.
You need to use WebClient or HttpWebRequest.
Exemple with web client =>
public void testSend()
{
string url = "";
string str = "test";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
string Data = "data=" + str;
byte[] postBytes = Encoding.UTF8.GetBytes(Data);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream resStream = response.GetResponseStream();
var sr = new StreamReader(response.GetResponseStream());
string responseText = sr.ReadToEnd();
WebClient client = new WebClient();
client.UploadStringCompleted += client_UploadStringCompleted;
client.UploadStringAsync(new Uri(url), responseText);
}
void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
if (e.Error != null)
{
// error in uploading
}
}

The stream does not allows seek operations. What are possible solutions?

public static string MakePOSTWebREquest(string url, string contentType, string postData)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
if (!String.IsNullOrEmpty(contentType))
{
req.ContentType = contentType;
}
else
{
req.ContentType = "application/x-www-form-urlencoded";
}
req.Method = "POST";
byte[] pbytes = Encoding.UTF8.GetBytes("list=");
byte[] arr = Encoding.UTF8.GetBytes(postData);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = arr.Length + pbytes.Length;
Stream stream = req.GetRequestStream(); //error stream does not allows seek operation
stream.Write(pbytes, 0, pbytes.Length);
stream.Write(arr, 0, arr.Length);
stream.Close();
HttpWebResponse webResp = (HttpWebResponse)req.GetResponse();
//Now, we read the response (the string), and output it.
Stream respStream = webResp.GetResponseStream();
GZipStream zStream = new GZipStream(respStream, CompressionMode.Decompress);
StreamReader reader = new StreamReader(zStream);
string respData = reader.ReadToEnd();
return respData;
}
I am getting an exception that says
This stream does not supports seek operation.
What are the possible solutions? This code runs for some time and after some time it gives a error message of The operation has timed out.

How to log in to Craigslist using C#

I'm using the following code to log into Craigslist, but haven't succeeded yet.
string formParams = string.Format("inputEmailHandle={0}&inputPassword={1}", "must_chd#yahoo.com", "removed");
//string postData = "inputEmailHandle=must_chd#yahoo.com&inputPassword=removed";
string uri = "https://accounts.craigslist.org/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.KeepAlive = true;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
byte[] postBytes = Encoding.ASCII.GetBytes(formParams);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookyHeader = response.Headers["Set-cookie"];
string pageSource;
string getUrl = "https://post.craigslist.org/del";
WebRequest getRequest = WebRequest.Create(getUrl);
getRequest.Headers.Add("Cookie", cookyHeader);
WebResponse getResponse = getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
{
pageSource = sr.ReadToEnd();
}
Use WebTest to record your login process, then generate the code. This will help you to understand what is wrong with YOUR code.

Categories