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

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.

Related

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

(400) Bad Request, can't get response (Custom minecraft launcher)

I just can't seem to be able to post a JSON to the webpage https://authserver.mojang.com/authenticate and get a response.
when I post the JSON it just says
The remote server returned an error: (400) Bad Request
I've gone through many different scripts by others and created my own by converting the Java code to C#. Anyway, here's the code that has worked the best so far.
string authserver = "https://authserver.mojang.com/authenticate";
byte[] rawData = fs.GetBytes(**[JSON]**);
WebRequest request = WebRequest.Create(authserver);
request.ContentType = "application/json";
request.Method = "POST";
//request.ContentLength = rawData.LongLength;
WebResponse connection = request.GetResponse();
connection.ContentType = "application/json";
connection.ContentLength = rawData.LongLength;
Stream stream = connection.GetResponseStream();
stream.Write(rawData, 0, rawData.Length);
stream.Flush();
byte[] rawVerification = new byte[10000];
int count = stream.Read(rawVerification, 0, 10000);
Edit:
is it possible to do this code with webclient?
Edit:
it had an invalid input, the json didn't have the correct data needed
try this:
WebRequest request = WebRequest.Create (authserver);
request.Method = "POST";
string postData = "YourJSON";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/json";
request.ContentLength = byteArray.Length;
using(Stream s = request.GetRequestStream ()){
s.Write (byteArray, 0, byteArray.Length);
}
WebResponse response = request.GetResponse ();
using(var dataStream = response.GetResponseStream ()){
using(StreamReader reader = new StreamReader (dataStream)){
string responseFromServer = reader.ReadToEnd ();
}
}
response.Close();
Essentially You shouldn't call getResponse() before you submit your data

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
}
}

How to send a string using Stream

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();
}

Port webbrowser control, form submit to httpwebrequest?

I would like to port a web browser form submittion to a httpwebrequest one.
its pretty simple:
wb.Document.GetElementsByTagName("input")[0].SetAttribute("value", "image.png");
wb.Document.GetElementsByTagName("input")[1].SetAttribute("value", ImageToBase64(p.Image));
wb.Document.GetElementsByTagName("form")[0].InvokeMember("submit");
I have tried the following, but the image file is currupted somehow...
string param = "name=image.jpg&file=" + ImageToBase64(p.Image);
post(param, "urlToPHPfileShown");
public string post(string param, string url)
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
string postArgs = param;
req.Method = "POST";
req.CookieContainer = new CookieContainer();
req.ContentType = #"application/x-www-form-urlencoded";
byte[] buffer = System.Text.ASCIIEncoding.UTF8.GetBytes(postArgs);
req.ContentLength = buffer.Length;
req.AllowAutoRedirect = true;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(buffer, 0, buffer.Length);
reqStream.Close();
}
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
using (Stream respStream = response.GetResponseStream())
{
using (StreamReader sr = new StreamReader(respStream))
{
string s = sr.ReadToEnd();
return s;
}
}
}
EDIT:
I just looked at the post with WireShark.
The one which is working seem to have lots of %2F instead of just / (slash).
So is there maybe a charset problem?

Categories