PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label httpwebrequest. Show all posts
Showing posts with label httpwebrequest. Show all posts

Monday, October 31, 2022

[FIXED] How to corectly set the ContentType property in HttpWebRequest (or how to fix the missing Content-Type header)

 October 31, 2022     content-type, contenttype, header, httpwebrequest     No comments   

Issue

I thought I'd share something that took me some time to figure out:

I wrote a simple Post method using HttpWebRequest class. In HttpWebRequest you can't use HttpWebRequest.Headers collection to set your desired headers when there is a dedicated property for it - you must use that dedicated property. ContentType is one of them. So I created my HttpWebRequest like this:

            HttpWebRequest httpWebRequest = (HttpWebRequest)webRequest;
            httpWebRequest.Method = "POST";
            httpWebRequest.KeepAlive = false;
            httpWebRequest.ServicePoint.Expect100Continue = false;
            httpWebRequest.ContentType = "application/json";

somewhere below I set the body of my request like this:

            using (StreamWriter streamWriter = new StreamWriter(streamWebRequest))
            {
                streamWriter.Write(sJson);
            }

and posted the request using:

            WebResponse webResponse = httpWebRequest.GetResponse();

But I kept getting a "400 - Bad Request" error, while the same request worked from Postman. After analyzing the request with Fiddler I found that when I send the request from my app, the Content-Type: application/json header is missing. All the other headers were present, except for Content-Type. I thought I'm setting it wrong, so I googled but didn't find a good answer. After much experimentation I found, that if I move the line:

            httpWebRequest.ContentType = "application/json"

after this block:

            using (StreamWriter streamWriter = new StreamWriter(streamWebRequest))
            {
                streamWriter.Write(sJson);
            }

then the httpWebRequest.ContentType = "application/json" header finally appears in the request. So, for HttpWebRequest make sure you always set your HttpWebRequest's body/content first, before you set the ContentType property.

Hope it helps


Solution

My question above already has the answer, but to mark it as "Answered" I had to add this comment:

Make sure you always set your HttpWebRequest's body/content first, before you set the ContentType property.This way the "Content-Type" header will appear in the request.



Answered By - Alexey Nagoga
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, May 19, 2022

[FIXED] How can I simulate a 'down' website?

 May 19, 2022     http, httpwebrequest, web-applications, web-services     No comments   

Issue

I am writing some failover code so that if my desktop app cannot connect to its website, it can instead try a backup website.

However, I cannot seem to figure out how to simulate a test if a website is 'down' or not. If I try an obvoiusly incorrect url such as "http://www.mybadsite.badTLD" , my ISP provider sends me to a default catch page.

Whereas when a site is truly down and you cannot connect to it, you get the browser message saying it cannot connect. This is what I need to emulate.

Thanks


Solution

Edit your hosts file to redefine the host you're trying to connect to. You can do 127.0.0.2 (Or anything unreachable).

You can also do a test with 0.0.0.0 - that returns a different error (Invalid IP). There may be some benefit to testing for that too.

Your ISP is redirecting for a DNS lookup failure, but anything resolved by your hosts file short-circuits that.



Answered By - Lee Kowalkowski
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, January 14, 2022

[FIXED] Building a web service: what options do I have?

 January 14, 2022     httpwebrequest, lamp, push-notification, rest, web-services     No comments   

Issue

I'm looking to build my first web service and I would like to be pointed in the right direction right from the start.

Here are the interactions that must take place:

  • First, a smartphone or computer will send a chunk of data to my web service.
  • The web service will persist the information to the database.
  • Periodically, an algorithm will access and modify the database.
  • The algorithm will periodically bundle data and send it out to smartphones or computer (how?)

The big question is: What basic things do I need to learn to in order to implement something like this?


Now here are the little rambling questions that I've also got rolling around in my head. Feel free to answer these if you wish. (...maybe consider them extra credit?)

  • I've heard a lot of good things about RESTful services, I've read the wiki article, and I've even played around with the Twitter's webservice which is RESTful. Is this the obvious way to go? Or should I still consider something else?
  • What programming language do I use to persist things to the database? I'm thinking php will be the first choice for this.
  • What programming language do I use to interact with the database? I'm thinking anything is probably acceptable, right?
  • Do I have to worry about concurrent access to the database, or does MySQL handle that for me? (I'm fairly new to databases too.)
  • How on earth do I send information back? Obviously, if it's a reply to an HTTP request that's no problem, but there will be times when the reply may take quite a long time to compute. Should I just let the HTTP request remain outstanding until I have the answer?
  • There will be times when I need to send information to a smartphone regardless of whether or not information has been sent to me. How can I push information to my users?

Other information that may help you know where I'm coming from:

  • I am pretty familiar with Java, C#, C++, and Python. I have played around with PHP, Javascript, and Ruby.
  • I am relatively new to databasing, but I get the basic idea.
  • I've already set up my server and I'm using a basic LAMP stack. My understanding of L, A, M and P is fairly rudimentary.

Solution

Language: Python for it's ease of use assuming the GIL is not a particular concern for your requirements (e.g. multi-threading). It has drivers for most databases and supports numerous protocols. There are several web frameworks for it - the most popular probably being Django.

Protocols: if you are HTTP focused study SOAP and REST. Note, SOAP tends to be verbose, which causes problems moving volumes of data. On the other hand, if you are looking at other options study socket programming and perhaps some sort of binary format such as Google's protocol buffers. Flash is also a possibility (see: Flash Remoting). Note, binary options require users install something onto their machine (e.g. applet or standalone app).

Replies: if the process is long running, and the client should be notified when it's done, I would recommend developing an app for the client. Browser's can be programmed with JavaScript to periodically poll, or a Flash movie can be embedded to real time updates, but these are somewhat tricky bits of browser programming. If you're dealing with wireless phones, look at SMS. Otherwise I would just provide a way for clients to get status, but not send out notification (e.g. push vs. pull). As @jcomea_ictx wrote, AJAX is an option if it's a browser based solution - study jQuery.

Concurrency: understand what ACID means with regards to databases. Think about what should happen if you receive multiple writes to the same data - database may not necessarily solve this problem the way you'd want.



Answered By - orangepips
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing